Home » Java » Core Java » Java 9 Streams API using JShell

About Martin Farrell

Martin Farrell
Martin is a Software Developer and Consultant specializing in Java and the Spring Framework. When not coding he spends time with his wife and two children, or cycling his bike.

Java 9 Streams API using JShell

This post looks at the Java 9 Streams API using JShell. The Streams API changes build on the success of Streams in Java 8, and introduce a number of utility methods – takeWhile, dropWhile and iterate. This post continues My Top Java 9 Features, and explores these methods using Jshell.

Streams API

The Streams API and Lambda’s were the most successful features of Java 8, and the changes in Java 9 build on this with some new utility methods

jshell> Stream.of(1,2,3,4,5).takeWhile(p->p<3).forEach(System.out::print);
12

Lets now return all values greater than 3, and we see the predicate instantly returns false and we get nothing returned

jshell> Stream.of(1,2,3,4,5).takeWhile(p -> p > 3).forEach(System.out::print);

jshell>
  • Unordered Lists – Longest list of values until predicate fails, although there may be values down Stream that satisfy the predicate and these wont be returned

We can see this below where the list only returns 2, even though the final element is 1, while the ordered list would have returned the 1 and the 2 –

jshell> Stream.of(2,3,6,5,1).takeWhile(p -> p < 3).forEach(System.out::print);
2

dropWhile (Predicate predicate)

dropWhile provides the opposite behaviour of takeWhile, so records are dropped while a predicate is true. As before we have similar considerations for sorted and unsorted lists.

  • Ordered Lists – Will return the longest list of records excluding those elements that satisfy the predicate
jshell> Stream.of(1,2,3,4,5).dropWhile(p -> p < 3).forEach(System.out::print);
345
  • Unordered Lists – Will drop the first records that satisfy the predicate –
jshell> Stream.of(2,3,6,5,1).dropWhile(p -> p < 3).forEach(System.out::print);
3651

jshell> Stream.of(1,2,3,5,6).dropWhile(p -> p < 3).forEach(System.out::print);
365

dropWhile/takeWhile Conclusions

The conclusion is that you need to take care when working with unordered list, unless the side effects are acceptable in your code. Although I cant think of a use case where I could accept the random element of unordered lists, I am sure some exist.

iterate (T seed,Predicate hasNext,UnaryOperator next)

This operates in a similar way to a for-loop. Taking a start value(T seed), exit condition(Predicate
hasNext) and whether we have a next value(Predicate
hasNext)

The iterate method has a exit condition attached –

jshell> Stream.iterate(1, i -> i < 6, i -> i + 1).forEach(System.out::println);
1
2
3
4
5

Conclusion

dropWhile and takeWhile present some useful utility methods for the Java Streams API. The major implication being whether your streams is ordered or unordered. The Stream.iterate method allows us to have for-loop functionality inside a Stream. I look forward to hearing peoples experiences with these new methods.

Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Java 9 Streams API using JShell

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of