introduce sorted(Comparator)

This commit is contained in:
Gregory.Shrago
2017-11-07 04:11:26 +03:00
parent 5495714182
commit 9ec08698c8
2 changed files with 19 additions and 2 deletions
@@ -478,6 +478,12 @@ public class TreeTraverserTest extends TestCase {
assertEquals(Arrays.asList(1, 2, 3, 4, 5), uniqueMod5.toList()); // same results again
}
public void testSorted() {
JBIterable<Integer> it1 = JBIterable.generate(1, INCREMENT).take(30);
JBIterable<Integer> it2 = JBIterable.generate(30, o -> o - 1).take(30).sorted(Integer::compareTo);
assertEquals(it1.toList(), it2.unique().toList());
}
// TreeTraversal ----------------------------------------------
@NotNull
@@ -844,15 +844,26 @@ public abstract class JBIterable<E> implements Iterable<E> {
}
/**
* Collects all items into an {@link ArrayList} and returns them as the new {@code JBIterable}.
* Collects all items into an {@link ArrayList} and returns it wrapped in a new {@code JBIterable}.
* @see JBIterable#collect(Collection)
*/
@NotNull
public final JBIterable<E> collect() {
if (content instanceof ArrayList) return this;
if (content instanceof Collection) return this;
return collect(ContainerUtilRt.<E>newArrayList());
}
/**
* Collects all items into an {@link ArrayList}, sorts it and returns it wrapped in a new {@code JBIterable}.
* @see JBIterable#collect(Collection)
*/
@NotNull
public final JBIterable<E> sorted(@NotNull Comparator<E> comparator) {
ArrayList<E> list = addAllTo(ContainerUtilRt.<E>newArrayList());
Collections.sort(list, comparator);
return from(list);
}
/**
* Returns a {@code List} containing all the elements from this iterable in
* proper sequence.