其他分享
首页 > 其他分享> > Collection接口(接口中常用的方法)

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

作者:互联网

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

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