Collection接口的方法的使用

1.add():将元素添加到集合coll中
2.size:获取添加元素的个数
3.addAll():将coll1中集合的元素添加到当前的集合中
4.clear;清空集合元素
5.isEmpty():判断当前集合是否为空(是否有元素)
@Test
    public void test1(){
        Collection coll = new ArrayList();
        //1.add():将元素添加到集合coll中
        coll.add("AA");
        coll.add("bb");
        coll.add("AA");
        coll.add(123);
        coll.add(new Date());

        //2.size:获取添加元素的个数
        System.out.println(coll.size());

        //3.addAll():将coll1中集合的元素添加到当前的集合中
        Collection coll1 = new ArrayList();
        coll1.add(456);
        coll.add("cc");
        coll.addAll(coll1);

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

        //4.clear;清空集合元素
        coll.clear();

        //5.isEmpty():判断当前集合是否为空(是否有元素)
        System.out.println(coll.isEmpty());
    }
6.contains(Object obj):判断当前集合中是否包含obj
7.CollectionAll(Collection coll1):判断形参coll1中的所有有元素是否都存在当前集合中
@Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("px"));
        coll.add(false);
        coll.add(new Person("mm",19));
//        Person p = new Person("mm",19);
//        coll.add(p);

        //6.contains(Object obj):判断当前集合中是否包含obj
        boolean contains = coll.contains(123);
        System.out.println(contains);//true
        System.out.println(coll.contains(new String("px")));//true
        //System.out.println(coll.contains(p));//true,不管是equals还是==,都比较的是p
        System.out.println(coll.contains(new Person("mm",19)));//false,此处是equals,比较内容-->true

        //7.CollectionAll(Collection coll1):判断形参coll1中的所有有元素是否都存在当前集合中
        Collection coll1 = Arrays.asList(123,4567);
        System.out.println(coll.containsAll(coll1));
    }
8.remove(Object obj):从当前集合中移除obj元素
9.removeAll(Collection coll1):从当前集合中移除coll1的搜友元素
10.retainAll(Collection coll1):交集,获取当前集合与从coll1集合的交集,并返回给当前集合
11.equals(Object obj):要想返回true,需要当前集合与形参集合的元素相同
@Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("px"));
        coll.add(false);
        coll.add(new Person("mm",19));

        //10.retainAll(Collection coll1):交集,获取当前集合与从coll1集合的交集,并返回给当前集合
//        Collection coll1 = Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println("coll1=" + coll1);
//        System.out.println("coll=" + coll);

        //11.equals(Object obj):要想返回true,需要当前集合与形参集合的元素相同
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new String("px"));
        coll1.add(false);
        coll1.add(new Person("mm",19));

        System.out.println(coll.equals(coll1));
    }
12.hasCode():返回当前对象的哈希值
13.集合-->数组:toArray()
14.iterator():返回iterator的实例,用于遍历集合元素
@Test
    public void test5(){
        Collection coll= new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("px"));
        coll.add(false);
        coll.add(new Person("mm",19));

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

        //13.集合-->数组:toArray()
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //数组-->集合:调用Array类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(list);

        List arr1 = Arrays.asList(123, 456);
        System.out.println(arr1.size());//

        List<int[]> list1 = Arrays.asList(new int[]{123, 456});
        System.out.println(list1.size());

        List<Integer> list2 = Arrays.asList(new Integer[]{123, 456});
        System.out.println(list2.size());

        //14.iterator():返回iterator的实例,用于遍历集合元素
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东北炸鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值