That Java Guy

  • Home
  • Contact
  • About

Java 9: What's new and interesting. Stream API improvements

by That Java Guy November 04, 2017 Collectors Java Stream
Stream API received a few nice additions with the Java 9. Both Stream interfaces as well as Collectors were updated. Let's start with the new collectors. To show these improvements we will use next classes:

Flatmapping and filtering collectors

Two new collectors were added to Collectors. Both of them are intended to be used for multilevel reduction, mostly for downstream collecting with Collectors#groupingBy() and Collectors#partitioningBy(). Two collectors that we want to look at are:

  • Collectors#filtering() - receives a filtering predicate and a downstream collector. Applies a filter and collects the filtered items with a provided collector.
  • Collectors#flatMapping() - receives a mapper and a downstream collector. Applies a flat mapping operation, using the mapper and then collects the mapped items with a provided collector.

Their names are pretty descriptive about what they do. Here are a few examples using these new collectors as well as the code without them but which will produce the same results:
These collectors can be applied to simple collect operations as well:

New methods in Stream

All stream interfaces (Stream/IntStream/LongStream/DoubleStream) received next new methods:
  • takeWhile - allows to provide a predicate which will determine when to stop processing stream elements.
  • dropWhile - allows to provide a predicate which will determine till when stream elements will be skipped and not processed.
  • iterate - it's an additional overload of previously present methods, which receives one more parameter. this new parameter is a predicate which determines when to terminate a stream. It allows to create finite streams opposed to previous methods which could crate only infinite streams. The intention of this method is to provide a functional-stream alternative of a for loop with the index.
Besides that Stream interface also received Stream#ofNullable() method which receives on element and if it is a null then an empty stream will be returned (Stream#empty()) or a stream of that one element otherwise. This method might be very useful in cases where some Stream concatenation occurs and some elements might be null, but nulls shouldn't be present. 
Here are a few examples using these new methods:

Conclusions 

All of these additional collectors and stream methods might become quite handy, just one thing that I'd like to add - don't rush and change all your for loops with the iterate+foreach methods use them if it feels appropriate or is needed.

To be continued... 

READ MORE
SHARE :

About Me




Marko Bekhta


Software engineer.


Follow Me

Labels

1z0-803 1z0-804 1z0-810 Bean Validation Certification Code Europe code generation Collections Collectors conference Enum exam Hibernate Hibernate Validator Immutables Java JPA MapStruct OCA OCP Oracle Stream validation
  • Popular
  • Recent
  • Comments
    JPA: Mapping Enums the right way
    Bean Validation: Validation based on multiple properties
    JPA: Ways to map many-to-many relationships with additional information

Instagram

About

Popular Posts

  • JPA: Mapping Enums the right way
    There are quite a few different approaches one can take to map enums in entities. I would separate them into three groups: The ones which ...
  • Bean Validation: Validation based on multiple properties
    Quite often there is a need to apply validation to some of the bean's properties based on the state of the other properties of the s...
  • JPA: Ways to map many-to-many relationships with additional information
    In this post we will take a look at couple of ways how many-to-many relationships, which have additional information, can be mapped to entit...
  • JPA: a handy utility for Enums to combine with AttributeConverter
    As it was discussed in one of the previous posts JPA: Mapping Enums the right way , JPA 2.1 standardized  AttributeConverter s, which can, f...
  • Java 9: What's interesting and new. Collection API
    Java 9 brings a lot of big new features, tools and API enhancements. But somehow the most excitement comes from a few simple additions to...

Labels

  • Certification
  • Code Europe
  • Java
  • Oracle
  • conference
  • exam

Template Created By : ThemeXpose . All Rights Reserved.