求ArrayList集合的交集 并集 差集 去重复并集

需要用到List接口中定义的几个方法:

  • addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 实例代码:
  • retainAll(Collection<?> c): 仅保留此列表中包含在指定集合中的元素。
  • removeAll(Collection<?> c) :从此列表中删除指定集合中包含的所有元素。
public class SetMathematics_example {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<Integer>();
        list1.addAll(Arrays.asList(1,2,3,4));
        List<Integer> list2 = new ArrayList<Integer>();
        list2.addAll(Arrays.asList(2,3,4,5));
        // 并集
        // list1.addAll(list2);

        // 交集
        // list1.retainAll(list2);

        // 差集
        // list1.removeAll(list2);

        // 无重复并集
        list2.removeAll(list1);
        list1.addAll(list2);
        for (Integer i : list1) {
            System.out.print(i+ " ");
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值