DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Trending

  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • Kafka With Spring Boot: Introduction
  • AI-Driven Integration in Large-Scale Agile Environments

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Java 9 Streams API Using JShell

Java 9 Streams API Using JShell

Let's dive into JShell and, in particular, see how it's used to interact with some of Java 9's newest features, including the updates to the Streams API.

By 
Martin Farrell
·
Updated Nov. 07, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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 lambdas were the most successful features of Java 8, and the changes in Java 9 build on those foundations with some new utility methods:

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


Let's now return all values greater than 3. 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>


  • Note for Unordered Lists: Longest list of values until predicate fails, although there may be values downstream that satisfy the predicate. These won't 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 behavior 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 lists unless the side effects are acceptable in your code. Although I can't 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. Take a start value (T seed), exit condition (Predicate hasNext), and whether we have a next value (Predicate hasNext)

The iterate method has an exit condition attached:

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


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

API Stream (computing) Java (programming language) JShell

Published at DZone with permission of Martin Farrell. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java
  • Techniques You Should Know as a Kafka Streams Developer

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook