专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Collection接口(接口中常用的方法)

Collection接口(接口中常用的方法)

70_1.png
因为Collection是接口,所以使用

Collection coll = new ArrayList();

来进行Collection中的方法的验证

add(Object e)

将元素e添加到集合coll中

public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add("Lisa");
        coll.add(new Date());

        System.out.println(coll);//[123, Lisa, Mon Jul 27 16:07:07 CST 2020]
    }

size()

获取添加的元素的个数

System.out.println(coll.size());//3

addAll(Collection coll)

将coll集合中的元素添加到当前的集合中

public void test2(){
        Collection coll = new ArrayList();
        coll.add(111);
        coll.add("Lisa");

        Collection collection = new ArrayList();
        collection.add("Tom");

        collection.addAll(coll);
        System.out.println(collection);//[Tom, 111, Lisa]
        System.out.println(collection.size());//3
    }

isEmpty()

判断当前集合是否为空

System.out.println(collection.isEmpty());//false

clear()

清空集合元素

collection.clear();
System.out.println(collection);//[]

contains(Object obj)

判断当前集合中是否包含obj

  • 判断时会调用obj对象所在类的equals方法

    String类重写了equals方法,所以比较的是内容;自定义类没有重写,如果想要比较内容,需要重写equals方法

  • 像Collection接口的实现类的对象中添加数据obj时,要求obj所在的类要重写equals()
class Students {
    private String name;
    private int age;
    private double grade;

    public Students(){

    }

    public Students(String name, int age, double grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                '}';
    }

    //重写equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Students students = (Students) o;
        return age == students.age &&
                Double.compare(students.grade, grade) == 0 &&
                Objects.equals(name, students.name);
    }
}

public void test3(){
        Collection coll = new ArrayList();
        coll.add(new Students("Tom",21,90));
        coll.add(new Students("Jack",20,100));
        coll.add(new Students("Lisa",19,80));
        coll.add(new Students("Ann",22,70));
        coll.add("殷志源");
        coll.add(123);

        System.out.println(coll.contains("殷志源"));
        System.out.println(coll.contains(123));
        System.out.println(coll.contains(new Students("Lisa", 19, 80)));
        System.out.println(coll.contains(new Students("Dan", 19, 80)));

    }

运行结果

70_2.png

第一个是String类型比较,String类型重写了equals()方法,所以比较的是内容

第二个是int数据类型进行比较

第三四个都是自定义的类的对象进行比较,需要在自定义类中重写equals()方法

containsAll(Collection coll)

判断形参coll1中的所有元素是否都存在于当前集合中

public void test4(){
        Collection coll = new ArrayList();
        coll.add(new Students("Tom",21,90));
        coll.add(new Students("Jack",20,100));
        coll.add(new Students("Lisa",19,80));
        coll.add(new Students("Ann",22,70));

        Collection coll1 = new ArrayList();
        coll1.add(new Students("Tom",21,90));
        coll1.add(new Students("Jack",20,100));
        coll1.add(new Students("Lisa",19,80));
        coll1.add(new Students("Ann",22,70));
        coll1.add("殷志源");
        coll1.add(123);

        System.out.println(coll1.containsAll(coll));//true
    }

remove(Object obj)

从当前集合中去删除obj元素,删除成功返回true,删除失败返回false

//coll中存在
System.out.println(coll.remove(new Students("Tom", 21, 90)));//true
//coll中不存在
System.out.println(coll.remove(new Students("Tom", 21, 10)));//false

removeAll(Collection coll)

从当前集合中移除coll1中所有的元素(差集)

System.out.println(coll1.removeAll(coll));
System.out.println(coll1);//[殷志源, 123]

retainAll(Collection coll)

获取当前集合和coll这个集合的交集,并修改当前集合为交集结果

 public void test5(){
        Collection coll = new ArrayList();
        coll.add("Ann");
        coll.add(123);
        coll.add(456);

        Collection coll1 = new ArrayList();
        coll1.add("Ann");
        coll1.add("Jack");
        coll1.add("Tom");
        coll1.add(123);
        coll1.add(456);

        System.out.println(coll1.retainAll(coll));//true
        System.out.println(coll1);//[Ann, 123, 456]
    }

equals(Object obj)

要想返回true,要求当前集合和形参集合的元素相同

//此时的coll1中的内容和coll中的相同
System.out.println(coll1.equals(coll));//true

hashCode()

返回当前对象的哈希值

System.out.println(coll.hashCode());//63445645

数组和集合之间的数据转换

数组—>集合

调用Arrays类的静态方法asList()

Integer[] arr = new Integer[]{123,456,789};
Collection collection = new ArrayList();
collection = Arrays.asList(arr);
ystem.out.println(collection);//[123, 456, 789]

集合—>数组

toArray()方法

Collection coll = new ArrayList();
coll.add("Tom");
coll.add("Jack");
coll.add("Ann");
Object[] objects = coll.toArray();
System.out.println(Arrays.toString(objects));//[Tom, Jack, Ann]

iterator()

返回Iterator接口的实例,用于遍历集合元素

Iterator iterator = coll.iterator();

具体遍历过程详见遍历集合元素

未经允许不得转载:搜云库技术团队 » Collection接口(接口中常用的方法)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们