尚硅谷-集合笔记2

本文主要测试了Java中Collection接口的contains(), containsAll(), remove(), removeAll(), retainAll(), equals()以及hashCode()等方法。通过示例代码说明了equals()方法在集合操作中的关键作用,特别是对于对象的比较和移除。同时,文章还涉及了将集合转换为数组和数组转换为集合的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.atgugui.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Collection接口中声明的方法的测试
 * <p>
 * 结论:
 * 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()
 *
 * @author WZ
 * @create 2021-06-20 17:28
 */
public class CollectionTest {
    @Test
    public void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);

//        Person p=new Person("Jerry",20);
//        coll.add(p);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);


        //1.contains(Object obj):判断当前这个集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()。
        boolean contains = coll.contains(123);
        System.out.println(contains);//true

        System.out.println(coll.contains(new String("Tom")));//true 这个调用的是equals String已经重写过了,比的是内容。
//        System.out.println(coll.contains(p));//true
        System.out.println(coll.contains(new Person("Jerry", 20)));//fasle 这个Person类中没有重写equals()方法,所以调用的是Object中的equals是==;
        // 重写后就是true了,重写后相当于new Person("Jerry", 20)的对象调用equals方法,一个一个与123/456等等比,因为ArrayList是有序的


        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
        Collection coll1 = Arrays.asList(123, 4567);//Arrays.asList()返回值是一个List,而List又是Collection的子接口,所以是多态的形式。
        System.out.println(coll.containsAll(coll1));//false


    }

    @Test
    public void test2() {
        //3.remove(Object obj):从当前集合中移除obj元素
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        boolean remove = coll.remove(123); //remove 里面肯定调用了equals()方法,先判断里面有没有123然后再移除。
        System.out.println(remove);//true
        System.out.println(coll);//此时里面的123就没了

        coll.remove(new Person("Jerry", 20));//在Person类中重写equals()方法后就可以进行移除了,详情参见上面containsAll()方法

        //4.removeAll(collection coll1):差集:从当前集合中移除coll1中所有的元素 (实际上移除的就是coll 和 coll1 的交集)
        Collection coll1 = Arrays.asList(123, 4567);//把7去掉,则下面输出的456也没了
        coll.removeAll(coll1);//其实就是把这个coll做了一个修改
        System.out.println(coll);//[456, Tom, false]


    }

    @Test
    public void test3() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
//        Collection coll1= Arrays.asList(123,456,789);
//        coll.retainAll(coll1);//其实这个也是对coll做了一个修改;这个方法求的是交集
//        System.out.println(coll);//[123, 456]

        //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
        // 一个一个去比较,但是是有序的因为ArrayList。即使元素一样但是顺序不一样也是false
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new Person("Jerry", 20));
        coll1.add(new String("Tom"));
        coll1.add(false);
        System.out.println(coll.equals(coll1));//true


    }

    @Test
    public void test4() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        //7.hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());//-1200490100

        //8.集合--->转化为数组:toArray():
        Object[] objects = coll.toArray();//注意返回的是object类型的数组,因为添加的123,456等是object类型
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);//就是上面的123,456.......
        }

        //拓展:数组--->转化成集合?:调用Arrays类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});//List正好是替换的数组
        //因为调用Arrays.asList()里面的参数可以是一个数组,但是返回值是一个List,而List又是Collection的子集。所以返回值肯定是一个集合
        System.out.println(list);//[AA, BB, CC]

        //Arrays.asList()用此转化为集合时有风险
        List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
        System.out.println(arr1);//[[I@22927a81] 输出的这个表示:这里面就一个元素,一个一维的数组里面是int类型
        //这个相当于把 new int[]{123, 456} 这个整体结构当成一个元素了---主要是因为int是基本数据类型所以才把数组当成元素来看
        System.out.println(arr1.size());//1
        List arr2 = Arrays.asList(123, 456);//这样写就是两个元素了
        System.out.println(arr2);//[123, 456]
        System.out.println(arr2.size());//2

        List arr3 = Arrays.asList(new Integer[]{123, 456});
        System.out.println(arr3);//[123, 456],调用size--(arr2.size());//2

        //9.interator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.Java中测试。


    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值