Skip to content

Commit

Permalink
Add toImmutableSortedBagBy to Collectors2
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Nagar <utkarshnagar2000@gmail.com>
  • Loading branch information
Utkarshn10 committed Sep 11, 2021
1 parent 12aed35 commit 5c9997c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,21 @@ private Collectors2()
Collector.Characteristics.UNORDERED);
}

/**
* <p>Returns the elements as a ImmutableSortedBag using the specified function.</p>
* <p>Examples:</p>
* {@code ImmutableSortedBag<Integer> bag1 = Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedBagBy(Object::toString));}<br>
* {@code ImmutableSortedBag<Integer> bag2 = Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedBagBy(Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toImmutableSortedBagBy(Function)}}
* </p>
* {@code ImmutableSortedBag<Integer> bag = Interval.oneTo(5).toImmutableSortedBagBy(Object::toString);}
*/
public static <T, V extends Comparable<? super V>> Collector<T, ?, ImmutableSortedBag<T>> toImmutableSortedBagBy(Function<? super T, ? extends V> function)
{
return Collectors2.toImmutableSortedBag(Comparators.byFunction(function));
}

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

@Test
public void toImmutableSortedBagBy()
{
MutableSortedBag<Integer> expected = SMALL_INTERVAL.toSortedBagBy(Object::toString);
ImmutableSortedBag<Integer> actual =
this.smallData.stream().collect(Collectors2.toImmutableSortedBagBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableSortedBag<Integer> actual2 = SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedBagBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toImmutableSortedBagByParallel()
{
MutableSortedBag<Integer> expected = LARGE_INTERVAL.toSortedBagBy(Object::toString);
ImmutableSortedBag<Integer> actual =
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedBagBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableSortedBag<Integer> actual2 = LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedBagBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toSortedSetWithComparator()
{
Expand Down

0 comments on commit 5c9997c

Please sign in to comment.