List of utility methods to do Collection Intersect
Set | intersect(Collection... collections) Returns the intersection set of a series of input collections. if (collections.length == 0) return new LinkedHashSet<A>(); Set<A> intersectionSet = new LinkedHashSet<A>(); intersectionSet.addAll(collections[0]); for (int i = 1; i < collections.length; i++) { intersectionSet.retainAll(collections[i]); return intersectionSet; ... |
List | intersect(Collection Returns a copy of a containing only elements also in b . List<T> result = new ArrayList<>(); for (T t : a) { if (b.contains(t)) { result.add(t); return Collections.unmodifiableList(result); |
Collection | intersect(Collection return a new Collection with the elements which appear both in a and b. Complexity: for sets - linear, for lists - quadratic. target.clear(); for (T elem : collA) { if (collB.contains(elem)) { target.add(elem); return target; |
Collection | intersect(Collection Given two collections of elements of type if (isEmpty(collection1) || isEmpty(collection2)) { return Collections.emptyList(); Set<T> intersection = new HashSet<T>(collection1); intersection.retainAll(collection2); return intersection; |
boolean | intersect(Collection intersect for (T t : t1) { if (t2.contains(t)) { return true; return false; |
Collection | intersectCollections(Collection extends T> collection1, Collection extends T> collection2) Returns all items that are both in collection1 and in collection2 Collection<T> result = new ArrayList<T>(collection1); result.retainAll(collection2); return result; |
boolean | intersectingCollections(Collection extends Object> collection1, Collection extends Object> collection2) intersecting Collections if (collection1 == null || collection2 == null) { return false; int size1 = collection1.size(); int size2 = collection2.size(); if (size1 < size2) { for (Object obj : collection1) if (collection2.contains(obj)) { ... |
C | intersectInto(C into, Collection intersect Into if (from.length > 0) { into.addAll(from[0]); for (int i = 1; i < from.length; i++) { into.retainAll(from[i]); return into; |
Set | intersection(Collection extends Collection extends T>> coll) Returns the intersection of all the collections given in a collection. Iterator<? extends Collection<? extends T>> i = coll.iterator(); if (!i.hasNext()) return new HashSet<T>(); Set<T> set = new HashSet<T>(i.next()); while (i.hasNext()) { Collection<? extends T> innerColl = i.next(); set.retainAll(innerColl); return set; |
Collection | intersection(Collection a, Collection b) intersection Collection results = new HashSet(); for (Iterator i = a.iterator(); i.hasNext();) { Object o = i.next(); if (b.contains(o)) { results.add(o); return results; ... |