/** * Returns distinct elements from a sequence by using * a specified {@link EqualityComparer} to compare values. */ public static <TSource> Enumerable<TSource> distinct( Enumerable<TSource> enumerable, EqualityComparer<TSource> comparer) { if (comparer == Functions.identityComparer()) { return distinct(enumerable); } final Set<Wrapped<TSource>> set = new HashSet<>(); Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer); Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper(); enumerable.select(wrapper).into(set); return Linq4j.asEnumerable(set).select(unwrapper); }
@Test public void testSemiJoin() { assertThat( EnumerableDefaults.semiJoin(EMPS, DEPTS, e -> e.deptno, d -> d.deptno, Functions.identityComparer()).toList().toString(), equalTo("[Emp(20, Theodore), Emp(20, Sebastian)]")); }
@Test public void testSemiJoin() { assertThat( EnumerableDefaults.semiJoin(EMPS, DEPTS, e -> e.deptno, d -> d.deptno, Functions.identityComparer()).toList().toString(), equalTo("[Emp(20, Theodore), Emp(20, Sebastian)]")); }
/** * Produces the set intersection of two sequences by * using the specified {@code EqualityComparer<TSource>} to compare * values. */ public static <TSource> Enumerable<TSource> intersect( Enumerable<TSource> source0, Enumerable<TSource> source1, EqualityComparer<TSource> comparer) { if (comparer == Functions.identityComparer()) { return intersect(source0, source1); } Set<Wrapped<TSource>> set0 = new HashSet<>(); Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer); source0.select(wrapper).into(set0); Set<Wrapped<TSource>> set1 = new HashSet<>(); try (Enumerator<Wrapped<TSource>> os = source1.select(wrapper).enumerator()) { while (os.moveNext()) { Wrapped<TSource> o = os.current(); if (set0.contains(o)) { set1.add(o); } } } Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper(); return Linq4j.asEnumerable(set1).select(unwrapper); }
/** * Produces the set union of two sequences by using a * specified EqualityComparer<TSource>. */ public static <TSource> Enumerable<TSource> union(Enumerable<TSource> source0, Enumerable<TSource> source1, final EqualityComparer<TSource> comparer) { if (comparer == Functions.identityComparer()) { return union(source0, source1); } Set<Wrapped<TSource>> set = new HashSet<>(); Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer); Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper(); source0.select(wrapper).into(set); source1.select(wrapper).into(set); return Linq4j.asEnumerable(set).select(unwrapper); }
/** * Produces the set difference of two sequences by * using the specified {@code EqualityComparer<TSource>} to compare * values. */ public static <TSource> Enumerable<TSource> except( Enumerable<TSource> source0, Enumerable<TSource> source1, EqualityComparer<TSource> comparer) { if (comparer == Functions.identityComparer()) { return except(source0, source1); } Set<Wrapped<TSource>> set = new HashSet<>(); Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer); source0.select(wrapper).into(set); try (Enumerator<Wrapped<TSource>> os = source1.select(wrapper).enumerator()) { while (os.moveNext()) { Wrapped<TSource> o = os.current(); set.remove(o); } } Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper(); return Linq4j.asEnumerable(set).select(unwrapper); }