Java中Collections的frequency方法


注:调用此方法时需要根据自己的须由复写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) {
        List<Student> list = new ArrayList<Student>();
        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);
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM