TSets { public static <T> Set<T> intersection(Set<T> a, Set<T> b) { if(a instanceof SortedSet && b instanceof SortedSet) { return sortedIntersection((SortedSet<T>) a, (SortedSet<T>) b); } else { Set<T> intersection = Sets.newLinkedHashSet(); Set<T> smaller = a.size() <= b.size() ? a : b; Set<T> larger = Sets.newHashSet(a.size() > b.size() ? a : b); for (T element : smaller) { if(larger.contains(element)) { intersection.add(element); } } return intersection; } }  static Set<T> intersection(Set<T> a, Set<T> b); static Set<Long> sequence(long from, long to); static Set<T> union(Set<T> a, Set<T> b);  }
@Test public void testSortedIntersectionReproCON_245() { Set<Long> a = Sets.newTreeSet(Arrays.<Long> asList(1424523725311000L, 1424523725341000L, 1424523725370000L, 1424523725421002L, 1424523725491000L, 1424523725514000L, 1424523725534000L, 1424523725565000L, 1424523725584003L, 1424523725605000L, 1424523725622002L, 1424523725641002L, 1424523725659002L, 1424523725676004L, 1424523725691000L, 1424523725710000L, 1424523725724000L, 1424523725738000L, 1424523725786002L, 1424523725801000L, 1424523725823002L, 1424523725841000L, 1424523725868000L, 1424523725895000L, 1424523725909000L, 1424523725924000L, 1424523725937002L, 1424523725963000L, 1424523725975002L, 1424523725987000L, 1424523725999003L, 1424523726012000L, 1424523726038000L, 1424523726050000L)); Set<Long> b = Sets.newTreeSet(Arrays.asList(1424523725341000L, 1424523725491000L, 1424523725514000L, 1424523725534000L, 1424523725584003L, 1424523725605000L, 1424523725641002L, 1424523725659002L, 1424523725691000L, 1424523725724000L, 1424523725753000L, 1424523725786002L, 1424523725801000L, 1424523725823002L, 1424523725841000L, 1424523725937002L, 1424523725963000L, 1424523725975002L, 1424523725987000L, 1424523725999003L, 1424523726012000L, 1424523726050000L)); Assert.assertEquals(TSets.intersection(a, b), Sets.intersection(a, b).copyInto(Sets.<Long> newHashSet())); }
@Test public void testIntersection() { Set<Integer> a = Sets.newHashSet(); Set<Integer> b = Sets.newHashSet(); populate(a); populate(b); Assert.assertEquals(TSets.intersection(a, b), Sets.intersection(a, b).copyInto(Sets.<Integer> newHashSet())); }
@Test public void testMixedIntersection() { Set<Integer> a = Sets.newTreeSet(); Set<Integer> b = Sets.newHashSet(); populate(a); populate(b); Assert.assertEquals(TSets.intersection(a, b), Sets.intersection(a, b).copyInto(Sets.<Integer> newHashSet())); }