728x90 AdSpace

Saturday, 24 June 2017

Java 8 Stream Filter with findAny orElse Example

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

Employee.java

package com.javatutorialcorner.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.javatutorialcorner.java8;

import java.util.Arrays;
import java.util.List;

public class StreamFindAny {

 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(), findAny(), orElse()");
  Employee result1 = employees.stream()                        // Convert to steam
                 .filter(x -> "Rahul Dravid".equals(x.getName()))        // we want "Rahul Dravid" only
                 .findAny()                                      // If 'findAny' then return found
                 .orElse(null);                                  // If not found, return null

     System.out.println(result1);

     Employee result2 = employees.stream()
              .filter(x -> "MS Dhoni".equals(x.getName()))
              .findAny()
              .orElse(null);

     System.out.println(result2);

    
 }

 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(), findAny(), orElse()
Employee {name : Rahul Dravid , age : 40}
Employee {name : MS Dhoni , age : 34}


Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 Stream Filter with findAny orElse Example Rating: 5 Reviewed By: Annamalai Thangaraj