用list实现集合的操作,交集,并集

用list实现集合的操作,交集,并集等。

这里写图片描述

图示从网上下载的
通过arraylist分别实现A,B,A+C这个集合。
/**
    * Removes from this list all of its elements that are contained in the
    * specified collection.
    *
    * @param c collection containing elements to be removed from this list
    * @return {@code true} if this list changed as a result of the call
    * @throws ClassCastException if the class of an element of this list
    *         is incompatible with the specified collection
    * (<a href="Collection.html#optional-restrictions">optional</a>)
    * @throws NullPointerException if this list contains a null element and the
    *         specified collection does not permit null elements
    * (<a href="Collection.html#optional-restrictions">optional</a>),
    *         or if the specified collection is null
    * @see Collection#contains(Object)
    */
   public boolean removeAll(Collection<?> c) {
       Objects.requireNonNull(c);
       return batchRemove(c, false);
   }
/**
     * Retains only the elements in this list that are contained in the
     * specified collection.  In other words, removes from this list all
     * of its elements that are not contained in the specified collection.
     *
     * @param c collection containing elements to be retained in this list
     * @return {@code true} if this list changed as a result of the call
     * @throws ClassCastException if the class of an element of this list
     *         is incompatible with the specified collection
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if this list contains a null element and the
     *         specified collection does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>),
     *         or if the specified collection is null
     * @see Collection#contains(Object)
     */
    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }

其他的方法经常用到,这个两个反而用的很少,所以只把这个两个的源码拿出来,并实现简单集合操作。
public boolean removeAll(Collection<?> c) 主要实现从test列表中删除在test1列表的数据。有兴趣的可以看看源码。
public boolean retainAll(Collection<?> c)说白了就是实现B操作。具体代码如下:

public class TestList {
    public static void main(String args[]) {
        ArrayList<String> test = new ArrayList<>();
        test.add("1");
        test.add("2");
        test.add("3");
        test.add("4");


        ArrayList<String> test1 = new ArrayList<>();
        test1.add("3");
        test1.add("4");
        test1.add("5");
        test1.add("6");
        test1.add("7");
        test1.add("8");

        test.retainAll(test1); //实现集合A

        test.removeAll(test1); //实现集合B

        test1.removeAll(test);
        test.addAll(test1);//实现A+B+C

        //实现A+C
        ArrayList<String> newTest = new ArrayList<>();
        newTest.addAll(test);
        newTest.addAll(test1);
        test.retainAll(test1);
        newTest.removeAll(test);
    }
}

邀请你加入,我们一起免费成长,改变只需一秒钟
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 8 中可以使用 Stream API 来实现集合之间的交集并集、差集、去重并集。 假设有两个 List 集合list1 和 list2,分别包含一些元素,代码如下: ```java List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5); List<Integer> list2 = Arrays.asList(4, 5, 6, 7, 8); ``` 下面分别介绍如何实现集合之间的操作。 1. 取交集交集即获取两个集合中共同拥有的元素。可以使用 Stream API 的 `filter()` 方法和 `contains()` 方法实现。代码如下: ```java List<Integer> intersection = list1.stream() .filter(list2::contains) .collect(Collectors.toList()); System.out.println("交集:" + intersection); // 输出 [4, 5] ``` 2. 取并集并集即获取两个集合中所有的元素,但是去重。可以使用 Stream API 的 `distinct()` 方法和 `concat()` 方法实现。代码如下: ```java List<Integer> union = Stream.concat(list1.stream(), list2.stream()) .distinct() .collect(Collectors.toList()); System.out.println("并集:" + union); // 输出 [1, 2, 3, 4, 5, 6, 7, 8] ``` 3. 取差集 取差集即获取两个集合中不同的元素。可以使用 Stream API 的 `filter()` 方法和 `!contains()` 方法实现。代码如下: ```java List<Integer> diff1 = list1.stream() .filter(e -> !list2.contains(e)) .collect(Collectors.toList()); System.out.println("差集1:" + diff1); // 输出 [1, 2, 3] List<Integer> diff2 = list2.stream() .filter(e -> !list1.contains(e)) .collect(Collectors.toList()); System.out.println("差集2:" + diff2); // 输出 [6, 7, 8] ``` 4. 去重并集 去重并集即获取两个集合中所有的元素,并且去重。可以使用 Stream API 的 `distinct()` 方法和 `flatMap()` 方法实现。代码如下: ```java List<Integer> distinctUnion = Stream.of(list1, list2) .flatMap(List::stream) .distinct() .collect(Collectors.toList()); System.out.println("去重并集:" + distinctUnion); // 输出 [1, 2, 3, 4, 5, 6, 7, 8] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术人Howzit

钱不钱的无所谓,这是一种鼓励!

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

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

打赏作者

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

抵扣说明:

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

余额充值