jdk8两个List取交集、差集、并集(不去重)、并集(去重)

jdk8两个List取交集、差集、并集(不去重)、并集(去重)

/**
     * 交集
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> intersect(List<String> list1, List<String> list2) {
        List<String> intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
        return intersect;
    }

    /**
     * 差集(list1-list2)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> minus(List<String> list1, List<String> list2) {
        List<String> minus = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
        return minus;
    }

    /**
     * 并集(不去重)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> unionAll(List<String> list1, List<String> list2) {
        list1.addAll(list2);
        return list1;
    }

    /**
     * 并集(去重)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> union(List<String> list1, List<String> list2) {
        list1.addAll(list2);
        return list1.stream().distinct().collect(Collectors.toList());
    }

demo代码:

public class TestSetOperation {

    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("a","b","c","d");
        List<String> list2 = Arrays.asList("c","d","e","f");

        //交集
        List<String> intersect = intersect(list1, list2);
        System.out.println("---交集 intersection---");
        intersect.stream().forEach(System.out::println);

        //差集(list1-list2)
        List<String> minus = minus(list1, list2);
        System.out.println("---差集 minus(list1-list2)---");
        minus.stream().forEach(System.out::println);

        //差集(list2-list1)
        //List minus2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
        List<String> minus2 = minus(list2,list1);
        System.out.println("---差集 minus(list2-list1)---");
        minus2.stream().forEach(System.out::println);

        //并集unionAll
        List<String> list3 = new ArrayList<String>(){{
            add("aa");
            add("bb");
            add("cc");
            add("dd");
        }};
        List<String> list4 = new ArrayList<String>(){{
            add("cc");
            add("dd");
            add("ee");
            add("ff");
        }};
        System.out.println("---并集 不去重---");
        List<String> unionAll = unionAll(list3,list4);
        unionAll.stream().forEachOrdered(System.out::println);

        //并集去重
        List<String> union = union(list3,list4);
        System.out.println("---并集 去重---");
        union.stream().forEachOrdered(System.out::println);
    }


    /**
     * 交集
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> intersect(List<String> list1, List<String> list2) {
        List<String> intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
        return intersect;
    }

    /**
     * 差集(list1-list2)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> minus(List<String> list1, List<String> list2) {
        List<String> minus = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
        return minus;
    }

    /**
     * 并集(不去重)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> unionAll(List<String> list1, List<String> list2) {
        list1.addAll(list2);
        return list1;
    }

    /**
     * 并集(去重)
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> union(List<String> list1, List<String> list2) {
        list1.addAll(list2);
        return list1.stream().distinct().collect(Collectors.toList());
    }
}

输出结果:

---交集 intersect---
c
d
---差集 minus(list1-list2)---
a
b
---差集 minus(list2-list1)---
e
f
---并集 不去重---
aa
bb
cc
dd
cc
dd
ee
ff
---并集 去重---
aa
bb
cc
dd
ee
ff

你可能感兴趣的:(java,list,java,算法)