Part 5 – Stream filter Java 8

fil

Stream filter Java 8

Java 8 Stream filter()

What have we learned so far

What have we learned so far

1. Functional Interface And Default Methods In Java 8 –
PART 1 : FUNCTIONAL INTERFACE AND DEFAULT METHODS IN JAVA 8

2. Lambda Expression in Java 8
PART 2 – LAMBDA EXPRESSION IN JAVA 8

3. Method Reference in Java 8
PART 3 – METHOD REFERENCE IN JAVA 8

4. Optional in Java 8
PART 4 – OPTIONAL IN JAVA 8

Stream filter()

filter allows you to filter the stream to process the stream.
Syntax : Stream<T> filter(Predicate<? super T> predicate)
findAny(0 will return the filtered object from the stream.
Lets see this with some examples :
Create a list of Student :

private static List<Student> createList() {

   Student student1 = new Student("Saurabh", "Pune", 26);
   Student student2 = new Student("Robert", "Pune", 25);
   Student student3 = new Student("John", "Mumbai", 21);
   Student student4 = new Student("Roman", "Pune", 18);
   Student student5 = new Student("Randy", "Mumbai", 17);
   
   return Arrays.asList(new Student[] {student1, student2, student3, student4, student5});
}

private static void filter(List<Student> students) {
   System.out.println("Executing filter() :");
   // Print all the students who lives in Pune
   System.out.println("*** Students who lives in Pune *** ");
   students.stream()
         .filter(student -> "Pune".equals(student.getCity()))
         .forEach(System.out::println);
   
   // Find the student whose name is Saurabh and return null if not found
   System.out.println("*** Students whos name is Saurabh *** ");
   Student stud = students.stream()
         .filter(student -> SAURABH.equals(student.getName()))
         .findAny()
         .orElse(null); // it will return null if filter does find any object matching the criteria 
   
   System.out.println(stud);
   System.out.println("Ending the execution of filter()");
}

Output :
Executing filter()
*** Students who lives in Pune ***
Student [name=Saurabh, city=Pune, age=26]
Student [name=Robert, city=Pune, age=25]
Student [name=Roman, city=Pune, age=18]
*** Students whos name is Saurabh ***
Student [name=Saurabh, city=Pune, age=26]
Ending the execution of filter()

Stream filter() Under the hood

filter method takes a predicate as an input which is an functional interface. Java will convert the above filter syntax into below lines of code by adding a Predicate functional interface.
Java converts the above filter code by adding a Predicate class and overrides the test method as shown below:

Student stud2 = students.stream()
   .filter( new Predicate<Student>() {
    public boolean test(Student student) {
     return SAURABH.equals(student.getName());
    }})
   .findAny()
   .orElse(null);

Source Code
Download source code of Java 8 features from below git repository :
java-8-features

Java 8 Features Tutorial

https://www.onlyfullstack.com/java-8-features-tutorial/


Lets go to our next tutorial where we will discuss below points :

map() vs flatMap() in Java 8
In this tutorial we will understand below topics – map() in Java 8
 – What is the use of flatMap() in Java 8?
 – map() vs flatMap()
 – flatMap() in Java 8
PART 6 – MAP() VS FLATMAP() IN JAVA 8