:
Content
01 Introduction to Stream API
02 Intermediate operation with example
03 Terminal operation with example
04 Difference between Intermediate & Terminal operation
05 Conclusion
Java 8 New Features
Stream API
The Stream API is used to process collections of objects. A stream
is a sequence of objects supporting various methods that can be
pipelined to produce the desired result.
Program Example
public class Demo {
public static void main(String[] args){
List<Integer> nums = Arrays.asList(8,9,7,4,5);
Stream<Integer> data = nums.stream();
Stream<Integer> sortedData = data.sorted();
sortedData.forEach( n-> System.out.println(n));
}
}
01
Intermediate
Operations
Intermediate Operations
These operations are used to pipeline other methods and to
transform them into other streams. They don't produce results
because these operation does not invoke until the terminal
operation gets executed.
filter(predicate) Method
It returns a new stream consisting of the elements of the stream from which it is
called which are according to the predicate (condition).
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a list of Integers
List<Integer> list = Arrays.asList(3, 4, 6, 12, 20);
// Getting a stream consisting of the
// elements that are divisible by 5
// Using Stream filter(Predicate predicate)
list.stream()
.filter(num -> num % 5 == 0)
.forEach(System.out::println);
}
}
sorted():
Returns a stream consisting of the elements of the stream passed, sorted according to
the natural order.
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a list of integers
List<Integer> list = Arrays.asList(-9, -18, 0, 25, 4);
System.out.println("The sorted stream is : ");
// displaying the stream with elements
// sorted in natural order
list.stream().sorted().forEach(System.out::println);
}}
map():
Mapper is a non-interfering, stateless function to apply to each element of the stream. It
returns a stream consisting of the results of applying the given function to the element of the
passed stream.
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
System.out.println("The stream after applying " + "the function is : ");
// Creating a list of Integers
List<Integer> list = Arrays.asList(3, 6, 9, 12, 15);
// Using Stream map(Function mapper) and
// displaying the corresponding new stream
list.stream().map(number -> number * 3).forEach(System.out::println);
}
}
02
Terminal
Operation
Terminal Operation
The operations which return non-stream values like primitive or object
or the collection or return nothing are called terminal operations.
Types Of Terminal Operation:
• collect(): • findFirst():
• reduce():
• forEach():
• count():
• min()
• findAny():
• max():
collect():
● The collect method is used to return the result of the intermediate operations
performed on the stream.
For example:-
reduce():
● The reduce method is used to reduce the elements of a stream to a single value.
For example:-
count():
min():
max():
findAny():
findFirst()
anyMatch (),AllMatch (),nonMatch()
forEach():
03
Difference between Intermediate
& Terminal operation
Differences b/w Intermediate and terminal
operations
Topics Covered
01 02 03 04
Introduction to Intermediate Terminal Difference
Stream API Operation Operation between them
References
● https://www.geeksforgeeks.org/stream-in-java/
● https://youtu.be/N3gQdIn90CI
● https://javaconceptoftheday.com/java-8-stream-intermediate-and-ter
minal-operations/
● https://www.geeksforgeeks.org/intermediate-methods-of-stream-in-j
ava
● https://blog.knoldus.com/operations-in-java-streams/
Thank you
Do you have any questions?
CRÉDITOS: Esta plantilla para presentaciones es una creación de Slidesgo, e incluye iconos
de Flaticon, e infografías e imágenes de Freepik
Por favor, conserva esta diapositiva para atribuirnos