Skip to content

Commit

Permalink
Add toImmutableSortedListBy to Collectors2.Closes #1136
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Nagar <utkarshnagar2000@gmail.com>

reword Add toImmutableSortedListBy to Collectors2.Closes#1136

Signed-off-by: Utkarsh Nagar <utkarshnagar2000@gmail.com>
  • Loading branch information
Utkarshn10 committed Sep 17, 2021
1 parent 22b5d9a commit db29e86
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,21 @@ private Collectors2()
EMPTY_CHARACTERISTICS);
}

/**
* <p>Returns the elements as a ImmutableList that has been sorted using the specified comparator.</p>
* <p>Examples:</p>
* {@code ImmutableList<Integer> list1 = Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedListBy(Object::toString));}<br>
* {@code ImmutableList<Integer> list2 = Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedListBy(Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toImmutableSortedListBy(Function)}}
* </p>
* {@code ImmutableList<Integer> list = Interval.oneTo(5).toImmutableSortedListBy(Object::toString);}
*/
public static <T, V extends Comparable<? super V>> Collector<T, ?, ImmutableList<T>> toImmutableSortedListBy(Function<? super T, ? extends V> function)
{
return Collectors2.toImmutableSortedList(Comparators.byFunction(function));
}

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

@Test
public void toImmutableSortedListBy()
{
MutableList<Integer> expected = SMALL_INTERVAL.toSortedListBy(Object::toString);
ImmutableList<Integer> actual = this.smallData.stream().collect(Collectors2.toImmutableSortedListBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableList<Integer> actual2 = SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedListBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toImmutableSortedListByParallel()
{
MutableList<Integer> expected = LARGE_INTERVAL.toSortedListBy(Object::toString);
ImmutableList<Integer> actual =
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedListBy(Object::toString));
Assert.assertEquals(expected, actual);
ImmutableList<Integer> actual2 = LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedListBy(Object::toString));
Assert.assertEquals(expected, actual2);
}

@Test
public void toSortedListWithComparator()
{
Expand Down

0 comments on commit db29e86

Please sign in to comment.