Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java 9 Improved Stream API

Java 9 Improved Stream API

Java 9 New Features Java 9 New Features

The improved Stream API in Java 9 adds some convenient methods that make stream processing easier and allow writing complex queries with collectors.

Java 9 adds several methods to Stream: dropWhile, takeWhile and ofNullable, and adds an overloaded version of the iterate method.

The takeWhile method

Syntax

default Stream takeWhile(Predicate predicate)

The takeWhile() method takes a predicate as an argument and returns a subset of the given Stream until the predicate first returns false. If the first value does not satisfy the predicate condition, an empty Stream is returned.

In an ordered Stream, takeWhile returns as many elements as possible starting from the beginning; in an unordered Stream, takeWhile returns the subset of elements from the beginning that satisfy the Predicate.

Example

import

java

.

util

.

stream

.

Stream

;

public

class

Tester

{

public

static

void

main

(

String

[

]

args

)

{

Stream

.

of

(

"

a

"

,

"

b

"

,

"

c

"

,

"

"

,

"

e

"

,

"

f

"

)

.

takeWhile

(

s

->!

s

.

isEmpty

(

)

)

.

forEach

(

System

.

out

::

print

)

;

}

}

In the above example, the takeWhile method stops the loop output when it encounters an empty string; the execution output is:

abc

The dropWhile method

Syntax

default Stream dropWhile(Predicate predicate)

The dropWhile method does the opposite of takeWhile: it takes a predicate as an argument and returns a subset of the given Stream only until the predicate first returns false.

Example

import

java

.

util

.

stream

.

Stream

;

public

class

Tester

{

public

static

void

main

(

String

[

]

args

)

{

Stream

.

of

(

"

a

"

,

"

b

"

,

"

c

"

,

"

"

,

"

e

"

,

"

f

"

)

.

dropWhile

(

s

-> !

s

.

isEmpty

(

)

)

.

forEach

(

System

.

out

::

print

)

;

}

}

In the above example, the dropWhile method starts the loop output when it encounters an empty string; the execution output is:

ef

The iterate method

Syntax

static  Stream iterate(T seed, Predicate hasNext, UnaryOperator next)

The method allows creating a sequential (possibly infinite) stream from an initial seed value, iteratively applying the specified next method. Iteration stops when the specified hasNext predicate returns false.

Example

java

.

util

.

stream

.

IntStream

;

public

class

Tester

{

public

static

void

main

(

String

[

]

args

)

{

IntStream

.

iterate

(

3

,

x

->

x

<
10
,
x
->
x
3
)

.

forEach

(

System

.

out

::

println

)

;

}

}

The execution output is:

3
6
9

The ofNullable method

Syntax

static  Stream ofNullable(T t)

The ofNullable method can prevent NullPointerExceptions by checking the stream to avoid null values.

If the specified element is non-null, it obtains the element and generates a single-element stream; if the element is null, it returns an empty stream.

Example

import

java

.

util

.

stream

.

Stream

;

public

class

Tester

{

public

static

void

main

(

String

[

]

args

)

{

long

count

=

Stream

.

ofNullable

(

100

)

.

count

(

)

;

System

.

out

.

println

(

count

)

;

count

=

Stream

.

ofNullable

(

null

)

.

count

(

)

;

System

.

out

.

println

(

count

)

;

}

}

The execution output is:


1
0

Java 9 New Features Java 9 New Features