Java 8 Stream Filter with map Example

In this article we are going to see about Java 8 Stream filter() , findAny() and orElse() with map() multiple conditions example program.

Employee.java

package com.ehowtonow.java8;

public class Employee {

 private String name;
 private int age;

 public Employee(String name, int age) {
  this.setName(name);
  this.setAge(age);
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }
 
 @Override
 public String toString() {
  return "Employee {name : "+name+" , age : "+age+"}";
 }
}

StreamFindAny.java

package com.ehowtonow.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamMap {

 public static void main(String[] args) {
  //Before Java 8
  System.out.println("Before Java 8");
  List employees = Arrays.asList(
                new Employee("Sachin Tendulkar", 41),
                new Employee("MS Dhoni", 34),
                new Employee("Rahul Dravid", 40),
                new Employee("Sourav Ganguly", 40)
        );


  Employee employee = getEmployeeByName(employees, "Rahul Dravid");
  
  System.out.println(employee);
  
  //Java 8 
  System.out.println("Using Java 8 filter(), map() return String ");
  String employeeName = employees.stream()                        // Convert to steam
                 .filter(x -> "Rahul Dravid".equals(x.getName()))        // we want "Rahul Dravid" only
                 .map(Employee::getName) 
                 .findAny()                                      // If 'findAny' then return found
                 .orElse(null);                                  // If not found, return null

     System.out.println(employeeName);

     System.out.println("Using Java 8 filter(), map() return List");
     List employeeNames = employees.stream()
        .map(Employee::getName) 
              .collect(Collectors.toList());

     employeeNames.forEach(System.out::println);

    
 }

 private static Employee getEmployeeByName(List employees, String name) {

  Employee result = null;
  for (Employee employee : employees) {
   if (name.equals(employee.getName())) {
    result = employee;
   }
  }
  return result;
 }
}

Output
Before Java 8
Employee {name : Rahul Dravid , age : 40}
Using Java 8 filter(), map() return String
Rahul Dravid
Using Java 8 filter(), map() return List
Sachin Tendulkar
MS Dhoni
Rahul Dravid
Sourav Ganguly


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java 8 Overview and New Features
  2. Java – BiConsumer Interface
  3. Java – DoubleStream.Builder Interface
  4. Java – BinaryOperator Interface
  5. Java – BooleanSupplier Interface
  6. Java – DoubleToLongFunction Interface
  7. Java – DoubleFunction Interface
  8. Java 8 Sort Map by Value
  9. Java – ToIntFunction Interface
  10. Java – LongUnaryOperator Interface
  11. Java – ToLongFunction Interface
  12. Java – IntConsumer Interface
  13. Java – ToLongBiFunction Interface
  14. Java 8 Stream Filter with multiple conditions Example
  15. Java – UnaryOperator Interface

Be the first to comment

Leave a Reply