day16 JavaSe之Collection接口成员方法

boolean addAll(Collection c):添加一个集合元素
boolean removeAll(Collection c):删除一个集合中包含的小集合中所有元素
boolean containsAll(Collection c):判断大集合是否包含小集合
boolean retainAll(Collection c):两个集合求交集,
这里的boolean是什么意思呢?
交集的元素去哪了呢?

import java.util.ArrayList;
import java.util.Collection;


public class CollectionDemo2 {
    public static void main(String[] args) {
        Collection c1 = new ArrayList();
        c1.add("hello");
        c1.add("world");
        c1.add("java");
        c1.add("hadoop");

        Collection c2 = new ArrayList();
        c2.add("hello");
        c2.add("world");
        c2.add("hive");
        c2.add("java");
        c2.add("hadoop");

        //boolean addAll(Collection c):添加一个集合元素
//        System.out.println(c1.addAll(c2));
//        System.out.println(c1);
//        System.out.println(c2);

        //boolean removeAll(Collection c):删除一个集合中包含的小集合中所有元素
//        System.out.println(c1.removeAll(c2));
//        System.out.println(c1);
//        System.out.println(c2);

        //boolean containsAll(Collection c):判断大集合是否包含小集合
//        System.out.println(c1.containsAll(c2));
//        System.out.println(c1);
//        System.out.println(c2);

        //boolean retainAll(Collection c):两个集合求交集,
        //假设有两个集合 一个c1一个c2
        //c1对c2做交集,最终的交集的结果保存在c1中,c2不变
        //返回值表示的c1是否发生了变化
        System.out.println(c1.retainAll(c2));
        System.out.println(c1);
        System.out.println(c2);


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值