Greetings! We have had a quick overview of Java 8 Streams API in the last post. We looked into the Power and simplicity of the Java 8 Streams API, brief about the Intermediate and the Terminal Operations over the streams, and different ways of building the streams (e.g from collections or numerical ranges etc.). In continuation to the same discussion, in this post, we will move ahead with the streams and have a look at the most important property of Java 8 Streams that is Laziness. If you are new to the concept of Java 8 streams, please go back and read Understanding Java 8 Streams API .
Java 8 Streams do not process the collection operations until user actually starts using it.
Short Circuit Methods:
Java 8 Streams API optimizes stream processing with the help of short circuiting operations. Short Circuit methods ends the stream processing as soon as their conditions are satisfied.
The operations like anyMatch, allMatch, noneMatch, findFirst, findAny, limit, and substream are such short-circuit methods in the Steams API.
Stream<String> namesStream = names.stream().map(n -> {
System.out.println("In map - " + n);
return n.toUpperCase();
}).filter(upperName -> {
System.out.println("In filter - " + upperName);
return upperName.startsWith("B");
});
namesStream.limit(2).collect(Collectors.toList());
Read full article from amitph.com > Java Tutorials: Java 8 Streams API - Laziness and Performance Optimization
Java 8 Streams do not process the collection operations until user actually starts using it.
Short Circuit Methods:
Java 8 Streams API optimizes stream processing with the help of short circuiting operations. Short Circuit methods ends the stream processing as soon as their conditions are satisfied.
The operations like anyMatch, allMatch, noneMatch, findFirst, findAny, limit, and substream are such short-circuit methods in the Steams API.
Stream<String> namesStream = names.stream().map(n -> {
System.out.println("In map - " + n);
return n.toUpperCase();
}).filter(upperName -> {
System.out.println("In filter - " + upperName);
return upperName.startsWith("B");
});
namesStream.limit(2).collect(Collectors.toList());
Read full article from amitph.com > Java Tutorials: Java 8 Streams API - Laziness and Performance Optimization
No comments:
Post a Comment