注:调用此方法时需要根据自己的须由复写Objects的equals方法
创建复写了equals方法的对象类
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object obj) { Student s = (Student) obj; return this.name == s.name && this.age == s.age; }}
调用
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class StudentTest { public static void main(String[] args) { Listlist = new ArrayList (); list.add(new Student("林青霞", 27)); list.add(new Student("风清扬", 30)); list.add(new Student("刘晓曲", 28)); list.add(new Student("武鑫", 29)); list.add(new Student("林青霞", 27)); int count=Collections.frequency(list, new Student("林青霞", 27)); System.out.println(count); }}