Java streams 35. Collect 11. Collectors.partitioningBy() collector

Terminal operation either returns one value (of the same or another type than the input type) or does not return anything at all (produces only side effects). It does not allow another operation to be applied afterward and closes the stream.

In this post, we will continue covering the last of the terminal operations called collect():

R collect(Collector<T,A,R> collector)

It is a specialization of the reduce() operation. It allows implementing a vast variety of algorithms using the ready-to-use collectors from the java.util.stream.Collectors class. We discussed how to create a custom collector in Java streams 25. Collect 1. Custom collector. In this article, we will use only the collectors produced by the Collectors class.



Creating Map object using Collectors.partitioningBy() collector

The Collectors.partitioningBy() collector puts the stream elements in one of two categories – one that contains elements that fit certain criteria and another that contains elements that do not fit that criteria.

There are two overloaded versions of factory methods that create the Collectors.partitioningBy() collector:

Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<T> predicate) – returns a Collector which partitions the input elements according to a Predicate, and organizes them into a Map<Boolean, List<T>>;

Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<T> predicate, Collector<T,A,D> downstream) – returns a Collector which partitions the input elements according to a Predicate, reduces the values in each partition according to another Collector, and organizes them into a Map<Boolean, D> whose values are the result of the downstream reduction.

The downstream is a collector that reduces the elements of each category.

The following are the examples of the Collectors.partitioningBy() usage: 

    
  Map<Boolean, List<String>> map1 = 
                    Stream.of("cat", "fish", "cat", "dog")
    .collect(Collectors.partitioningBy(s -> s.length() == 3));
  System.out.print(map1);   
            //prints: {false=[fish], true=[cat, cat, dog]}

  Map<Boolean, Set<String>> map3 = 
                     Stream.of("cat", "fish", "cat", "dog")
    .collect(Collectors.partitioningBy(s -> s.length() == 3, 
                                       Collectors.toSet()));
  System.out.print(map3);    
                  //prints: {false=[fish], true=[cat, dog]}

  Map<Boolean, String> map2 = 
                      Stream.of("cat", "fish", "cat", "dog")
    .collect(Collectors.partitioningBy(s -> s.length() == 3, 
                                 Collectors.joining(", ")));
  System.out.print(map2);    
                  //prints: {false=fish, true=cat, cat, dog}
     

As you can see, in the first example, the elements of each category are collected in a List by default. In the second example, the downstream collector puts them in a Set. And in the third example, the elements are concatenated by the joining collector.

In the next post, we will talk about creating a Map object using Collectors.mapping() collector (typically used as a parameter for Collectors.groupingBy() or Collectors.partitioningBy() collector).

See other posts on Java 8 streams and posts on other topics.
You can also use navigation pages for Java stream related blogs:
— Java 8 streams blog titles
— Create stream
— Stream operations
— Stream operation collect()
The source code of all the code examples is here in GitHub

, ,

Send your comments using the link Contact or in response to my newsletter.
If you do not receive the newsletter, subscribe via link Subscribe under Contact.

Powered by WordPress. Designed by Woo Themes