@Test void testIntersect() { TreeSet<Integer> result1 = CollectionUtils.intersect(asList(1, 2, 4, 5), asList(1, 3, 5, 7), TreeSet::new); TreeSet<Integer> result2 = CollectionUtils.intersect(emptyList(), asList(1, 3, 5, 7), TreeSet::new); List<Integer> result3 = CollectionUtils.intersect(singletonList(1), asList(1, 3, 5, 7), ArrayList::new); assertEquals(new TreeSet<>(asList(1, 5)), result1); assertEquals(new TreeSet<>(), result2); assertEquals(singletonList(1), result3); }
public static <T, C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) { C result = collectionBuilder.get(); HashSet<T> items = new HashSet<>(collection2); for (T next : collection1) { if (!items.add(next)) { result.add(next); } } return result; }
CollectionUtils { public static <T, C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) { C result = collectionBuilder.get(); HashSet<T> items = new HashSet<>(collection2); for (T next : collection1) { if (!items.add(next)) { result.add(next); } } return result; } }
CollectionUtils { public static <T, C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) { C result = collectionBuilder.get(); HashSet<T> items = new HashSet<>(collection2); for (T next : collection1) { if (!items.add(next)) { result.add(next); } } return result; } private  CollectionUtils(); }
CollectionUtils { public static <T, C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) { C result = collectionBuilder.get(); HashSet<T> items = new HashSet<>(collection2); for (T next : collection1) { if (!items.add(next)) { result.add(next); } } return result; } private  CollectionUtils(); static T merge(T collection1, T collection2, Supplier<T> factoryMethod); @SuppressWarnings("unchecked") static Collection<R> asCollection(Object potentialCollection); static C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder); }
CollectionUtils { public static <T, C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) { C result = collectionBuilder.get(); HashSet<T> items = new HashSet<>(collection2); for (T next : collection1) { if (!items.add(next)) { result.add(next); } } return result; } private  CollectionUtils(); static T merge(T collection1, T collection2, Supplier<T> factoryMethod); @SuppressWarnings("unchecked") static Collection<R> asCollection(Object potentialCollection); static C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder);  }