Intersection and union of ArrayLists in Java

后端 未结 24 2545
离开以前
离开以前 2020-11-22 06:05

Are there any methods to do so? I was looking but couldn\'t find any.

Another question: I need these methods so I can filter files. Some are AND filter

24条回答
  •  甜味超标
    2020-11-22 06:52

    In Java 8, I use simple helper methods like this:

    public static  Collection getIntersection(Collection coll1, Collection coll2){
        return Stream.concat(coll1.stream(), coll2.stream())
                .filter(coll1::contains)
                .filter(coll2::contains)
                .collect(Collectors.toSet());
    }
    
    public static  Collection getMinus(Collection coll1, Collection coll2){
        return coll1.stream().filter(not(coll2::contains)).collect(Collectors.toSet());
    }
    
    public static  Predicate not(Predicate t) {
        return t.negate();
    }
    

提交回复
热议问题