java.util.Collections.frequency()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(147)

本文整理了Java中java.util.Collections.frequency()方法的一些代码示例,展示了Collections.frequency()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collections.frequency()方法的具体详情如下:
包路径:java.util.Collections
类名称:Collections
方法名:frequency

Collections.frequency介绍

[英]Returns the number of elements in the Collection that match the Object passed. If the Object is null, then the number of null elements is returned.
[中]返回集合中与传递的对象匹配的元素数。如果对象为null,则返回null元素的数量。

代码示例

代码示例来源:origin: stackoverflow.com

List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s: mySet){

 System.out.println(s + " " +Collections.frequency(asList,s));

}

代码示例来源:origin: stackoverflow.com

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
  System.out.println(key + ": " + Collections.frequency(list, key));
}

代码示例来源:origin: stackoverflow.com

int count = Collections.frequency(party.values(), 1);
System.out.println(count);
===> 4

代码示例来源:origin: stackoverflow.com

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class stackexample {
  public static void main(String[] args) {
    List<Integer> values = Arrays.asList( 5, 0, 0, 2 );
    int occurrences = Collections.frequency(values, 0);
    System.out.println("occurrences of zero is " + occurrences); //shows 0 but answer should be 2
  }
}

代码示例来源:origin: stackoverflow.com

System.out.println(java.util.Collections.frequency(table.getValue(), "x"));

代码示例来源:origin: stackoverflow.com

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test{
  public static void main(String[] args) {
    String[] city = {"texas", "ny", "sydney", "ny", "paris", "texas", "ny"};
    List<String> asList = Arrays.asList(city);
    Map<String, Integer> map= new HashMap<String, Integer>();
    for(String s: city){
      map.put(s,Collections.frequency(asList,s));
    }
    System.out.println(map);

  }
}

代码示例来源:origin: stackoverflow.com

private void removeTheDuplicates(List<Customer>myList) {
  for(ListIterator<Customer>iterator = myList.listIterator(); iterator.hasNext();) {
    Customer customer = iterator.next();
    if(Collections.frequency(myList, customer) > 1) {
      iterator.remove();
    }
  }
  System.out.println(myList.toString());

}

代码示例来源:origin: stackoverflow.com

public int countPacks(String flavor) {
  int numOccurrences = java.util.Collections.frequency(packets, flavor);
  if(numOccurrences == 0) {
   System.out.println("You have not entered a correct flavor");
  }
  return numOccurrences;
}

代码示例来源:origin: stackoverflow.com

List<String>products = ...
List<String>cart = ...

for (String cartItem : cart) {
    int occurrences = Collections.frequency(products, cartItem);

    if (occurrences > 0) {
     System.out.println(cartItem + ": " + occurrences);
    }
}

代码示例来源:origin: stackoverflow.com

List<Integer> theList = Arrays.asList(1, 3, 4, 3, 4, 3, 2, 3, 3, 3, 3, 3);
Integer maxOccurredElement = theList.stream()
    .reduce(BinaryOperator.maxBy((o1, o2) -> Collections.frequency(theList, o1) -
            Collections.frequency(theList, o2))).orElse(null);
System.out.println(maxOccurredElement);

代码示例来源:origin: stackoverflow.com

int[] arr = {1, 2, 3};
Integer[] boxedArr = new Integer[arr.length];
for(int i = 0; i < arr.length; i++)
  boxedArr[i] = arr[i];
System.out.println(Collections.frequency(Arrays.asList(boxedArr), 1));

代码示例来源:origin: stackoverflow.com

Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet)
{
  System.out.println(temp + ": " + Collections.frequency(list, temp));
}

代码示例来源:origin: stackoverflow.com

List<Integer> freqList=new ArrayList<Integer>();
 // Add numebers to this list.
 for (int i = 10; i <20; i++) {
  int freq=Collections.frequency(freqList, i);
          // This will return frequency of number
   System.out.println(i+"  "+freq);  
 }

代码示例来源:origin: stackoverflow.com

int sum= 0;
for (Integer[] arr: list) {
  sum+=Collections.frequency(Arrays.asList(arr), 1);
}
System.out.println(sum);

代码示例来源:origin: stackoverflow.com

void countdigit(Integer[] x) {
  List<Integer> ints = Arrays.asList(x);
  for(int item : x) {
    int frequency = Collections.frequency(ints, item);
    System.out.println(frequency);
  }
}

代码示例来源:origin: stackoverflow.com

accnums[0] = 1;
accnums[1] = 2;
final int count = accnums.length
  - Collections.frequency(Arrays.asList(accnums), null);
System.out.println("You have used " + count + " slots");

代码示例来源:origin: stackoverflow.com

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class stackexample {
  public static void main(String[] args) {
    List<Integer> values = Arrays.asList( 5, 0, 0, 2 );
    int occurrences = Collections.frequency(values, 0);
    System.out.println("occurrences of zero is " + occurrences); //shows 0 but answer should be 2
  }
}

代码示例来源:origin: stackoverflow.com

List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s : mySet) {
  System.out.println(s + " " + Collections.frequency(asList, s));
}

代码示例来源:origin: stackoverflow.com

int[] data = new int[] { 5,0, 0, 1};
 List<Integer> intList = new ArrayList<Integer>();
 for (int index = 0; index < data.length; index++)
 {
   intList.add(data[index]);
 }
 int occurrences = Collections.frequency(intList, 0);
 System.out.println("occurrences of zero is " + occurrences);

代码示例来源:origin: stackoverflow.com

List<String> list = Arrays.asList(text.split(" "));
   Set<String> uniqueWords = new HashSet<String>(list);
   for (String word : uniqueWords) {
     System.out.println(word + ": " + Collections.frequency(list, word));
   }

相关文章

Collections类方法