StreamAPI 3
StreamAPI 3
Content
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.
01 02 03 04
● 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