Skip to content

Commit

Permalink
Add toImmutableSortedSetBy to Collectors2. Resolved #1137
Browse files Browse the repository at this point in the history
Signed-off-by: Ly (Harriet) Bui <lhbui@umass.edu>
  • Loading branch information
HarrietLyBui committed Sep 30, 2021
1 parent 2c162bc commit 3f398a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,21 @@ private Collectors2()
Collector.Characteristics.UNORDERED);
}

/**
* <p>Returns the elements as a ImmutableSortedSet using the specified function to compare each element.</p>
* <p>Examples:</p>
* {@code ImmutableSortedSet<Integer> set1 = Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedSetBy(Object::toString));}<br>
* {@code ImmutableSortedSet<Integer> set2 = Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedSetBy(Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toImmutableSortedSetBy(Function)}.
* </p>
* {@code ImmutableSortedSet<Integer> set = Interval.oneTo(5).toImmutableSortedSetBy(Object::toString);}
*/
public static <T, V extends Comparable<? super V>> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSetBy(Function<? super T, ? extends V> function)
{
return Collectors2.toImmutableSortedSet(Comparators.byFunction(function));
}

/**
* <p>Returns the elements as a MutableBag.</p>
* <p>Examples:</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,26 @@ public void toImmutableSortedSetParallel()
Assert.assertEquals(expected, actual2);
}

@Test
public void toImmutableSortedSetBy()
{
MutableSortedSet<Integer> expected = SMALL_INTERVAL.toSortedSetBy(Object::toString);
ImmutableSortedSet<Integer> actual = this.smallData.stream().collect(Collectors2.toImmutableSortedSetBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableSortedSet<Integer> actual2 = SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedSetBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toImmutableSortedSetByParallel()
{
MutableSortedSet<Integer> expected = LARGE_INTERVAL.toSortedSetBy(Object::toString);
ImmutableSortedSet<Integer> actual = this.bigData.parallelStream().collect(Collectors2.toImmutableSortedSetBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableSortedSet<Integer> actual2 = LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedSetBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toSortedBag()
{
Expand Down

0 comments on commit 3f398a7

Please sign in to comment.