ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-07-27 17:02:55  阅读:248  来源: 互联网

标签:常用 Students add Collection coll 接口 coll1 println new


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

因为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)));

    }

运行结果

第一个是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();

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

标签:常用,Students,add,Collection,coll,接口,coll1,println,new
来源: https://www.cnblogs.com/CrabDumplings/p/13386298.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有