Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of partitioningBy

For example, if I intend to partition some elements, I could do something like:

Stream.of("I", "Love", "Stack Overflow")       .collect(Collectors.partitioningBy(s -> s.length() > 3))       .forEach((k, v) -> System.out.println(k + " => " + v)); 

which outputs:

false => [I] true => [Love, Stack Overflow] 

But for me partioningBy is only a subcase of groupingBy. Although the former accepts a Predicate as parameter while the latter a Function, I just see a partition as a normal grouping function.

So the same code does exactly the same thing:

 Stream.of("I", "Love", "Stack Overflow")        .collect(Collectors.groupingBy(s -> s.length() > 3))        .forEach((k, v) -> System.out.println(k + " => " + v)); 

which also results in a Map<Boolean, List<String>>.

So is there any reason I should use partioningBy instead of groupingBy? Thanks

like image 738
user2336315 Avatar asked Jan 16 '15 22:01

user2336315


People also ask

What is partitioningBy in Java?

Collectors partitioningBy() method is a predefined method of java. util. stream. Collectors class which is used to partition a stream of objects(or a set of elements) based on a given predicate. There are two overloaded variants of the method that are present.

Which return type can be created by partitioningBy collector on Stream of string elements?

Collectors.partitioningBy() using a Predicate Each of the elements from the Stream are tested against the predicate, and based on the resulting boolean value, this Collector groups the elements into two sets and returns the result as Map<Boolean, List<T>> .

What is collectors in Java?

Java Collectors. Collectors is a final class that extends Object class. It provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc. Java Collectors class provides various methods to deal with elements.

Which method is used to create a logical partition using streams?

partitioningBy() method to the Stream of Color objects, with predicate condition as ' Color. isRed() ', results in 2 separate lists of objects being created.


2 Answers

partitioningBy will always return a map with two entries, one for where the predicate is true and one for where it is false. It is possible that both entries will have empty lists, but they will exist.

That's something that groupingBy will not do, since it only creates entries when they are needed.

At the extreme case, if you send an empty stream to partitioningBy you will still get two entries in the map whereas groupingBy will return an empty map.

EDIT: As mentioned below this behavior is not mentioned in the Java docs, however changing it would take away the added value partitioningBy is currently providing. For Java 9 this is already in the specs.

like image 53
Oron Avatar answered Oct 23 '22 08:10

Oron


partitioningBy is slightly more efficient, using a special Map implementation optimized for when the key is just a boolean.

(It might also help to clarify what you mean; partitioningBy helps to effectively get across that there's a boolean condition being used to partition the data.)

like image 45
Louis Wasserman Avatar answered Oct 23 '22 08:10

Louis Wasserman