diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java index f97c4230e..0fa30ef34 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java @@ -30,6 +30,7 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BinaryOperator; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.ToDoubleFunction; import java.util.function.ToIntFunction; @@ -357,4 +358,24 @@ public final class TCollectors { this.b = b; } } + + public static TCollector> partitioningBy(Predicate predicate, + TCollector downstream) { + BiConsumer acc = downstream.accumulator(); + return teeing(TCollector.of(downstream.supplier(), (res, el) -> { + if (!predicate.test(el)) { + acc.accept(res, el); + } + }, downstream.combiner(), downstream.finisher()), + TCollector.of(downstream.supplier(), (res1, el1) -> { + if (predicate.test(el1)) { + acc.accept(res1, el1); + } + }, downstream.combiner(), downstream.finisher()), + (fls, tr) -> Map.of(false, fls, true, tr)); + } + + public static TCollector>> partitioningBy(Predicate predicate) { + return partitioningBy(predicate, toList()); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java b/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java index 6d5b9be2f..4efe438fe 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -178,4 +179,12 @@ public class CollectorsTest { .collect(Collectors.teeing(Collectors.summingInt(String::length), Collectors.averagingInt(String::length), (sum, avg) -> sum / avg))); } + + @Test + public void partitioningBy() { + Map> grouped = IntStream.range(0, 10).boxed() + .collect(Collectors.partitioningBy(i -> i % 2 == 0, Collectors.toSet())); + assertEquals(Set.of(1, 3, 5, 7, 9), grouped.get(false)); + assertEquals(Set.of(0, 2, 4, 6, 8), grouped.get(true)); + } }