Java集合frequency()方法



描述

Java集合frequency(Collection<?>, Object) 方法用于获取指定集合中等于指定对象的元素个数。

声明

以下是java.util.Collections.frequency() 方法的声明。

public static int frequency(Collection<?> c, Object o)

参数

  • c − 这是用于确定o频率的集合。

  • o − 这是要确定其频率的对象。

返回值

NA

异常

NullPointerException − 如果c为null,则抛出此异常。

整数列表中元素出现次数计数示例

以下示例演示了Java集合frequency(Collection,Object)方法的使用。我们创建了一个包含一些整数的List对象,并打印了原始列表。使用frequency(Collection, T)方法,我们打印了特定元素的计数。

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,3,5,3));

      System.out.println("Initial collection value: " + list);
      // print the frequency of 3.
      int result = Collections.frequency(list, 3);
      System.out.println("3 is present: "+result + " times.");
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Initial collection value: [1, 2, 3, 3, 5, 3]
3 is present: 3 times.

字符串列表中元素出现次数计数示例

以下示例演示了Java集合frequency(Collection,Object)方法的使用。我们创建了一个包含一些字符串的List对象,并打印了原始列表。使用frequency(Collection, T)方法,我们打印了特定元素的计数。

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","C","C"));

      System.out.println("Initial collection value: " + list);
      // print the frequency of C.
      int result = Collections.frequency(list, "C");
      System.out.println("C is present: "+result + " times.");
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Initial collection value: [A, B, C, D, C, C]
C is present: 3 times.

对象列表中元素出现次数计数示例

以下示例演示了Java集合frequency(Collection,Object)方法的使用。我们创建了一个包含一些字符串的List对象,并打印了原始列表。使用frequency(Collection, T)方法,我们打印了特定元素的计数。

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam"),new Student(1, "Julie")));

      System.out.println("Initial collection value: " + list);
      // print the frequency of Julie.
      int result = Collections.frequency(list, new Student(1, "Julie"));
      System.out.println("Julie is present: "+result + " times.");
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 1, Julie ]]
Julie is present: 2 times.
java_util_collections.htm
广告