【CollectionDemo2】

package com.yjf.esupplier.common.test;

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

/**
 * @author shusheng
 * @description Collection接口成员方法
 * @Email shusheng@yiji.com
 * @date 2018/12/11 16:48
 */
public class CollectionDemo2 {

    public static void main(String[] args) {

        // 创建集合 1
        Collection c1 = new ArrayList();
        c1.add("abc1");
        c1.add("abc2");
        c1.add("abc3");
        c1.add("abc4");

        // 创建集合 2
        Collection c2 = new ArrayList();
        c2.add("abc4");
        c2.add("abc5");
        c2.add("abc6");
        c2.add("abc7");

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

        //boolean removeAll(Collection c):移除一个集合的元素
        //只要有一个元素被移除了,就返回 true。
        System.out.println("removeAll:" + c1.removeAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);

        //boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还
        //只有包含所有的元素,才叫包含
        System.out.println("containsAll:"+c1.containsAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);

        //取交集后赋给c1
        System.out.println("retainAll:"+c1.retainAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
    }
}

执行结果:

addAll:true
c1:[abc1, abc2, abc3, abc4, abc4, abc5, abc6, abc7]
c2:[abc4, abc5, abc6, abc7]
removeAll:true
c1:[abc1, abc2, abc3]
c2:[abc4, abc5, abc6, abc7]
containsAll:false
c1:[abc1, abc2, abc3]
c2:[abc4, abc5, abc6, abc7]
retainAll:true
c1:[]
c2:[abc4, abc5, abc6, abc7]

 

转载于:https://www.cnblogs.com/zuixinxian/p/10340770.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值