Site icon

Find object from arraylist of objects using lambda stream java 8 (example)

  1. Given the arraylist of user defined objects or POJO (let we have person class).
    • List <Person> personList
  2. Find or Search or Filter person object from list of person objects.
    1. We will create list of Person objects.
    2. We would like to find person object from list of person objects.
    3. We will filter person object on specified fields.
      • We will filter person object by firstName, age etc.

Program – find POJO from list of objects using lambda stream java 8.

1.) Person class:

package org.learn;

public class Person {
	public String firstName;
	public String lastName;
	public int age;
	public String contact;
	public Person(String firstName, String lastName,
				int age, String contact) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		this.contact = contact;
	}
	public String toString() {
	    return "[" + firstName + " " + lastName +
		       " " + age + " " +contact +"]";
	}
}

2.) FindObjectInList class: 

FindObjectInList class contains the list of person objects. We are performing the following operations on list of person objects.

  1. Find person object(s) having first name = “Mike”
  2. Find person object(s) having first name = “Mike” and age = 24
package org.learn;

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

public class FindObjectInList {
		
	public static void main(String[] args) {
		// Remove nulls from List
		List <Person> personList = Stream.of(
					new Person("Mike", "harvey", 34, "001894536"),
				  	new Person("Nick", "young", 75,  "005425676"),
					new Person("Jack", "slater", 21 ,"009654153"),
					new Person("gary", "hudson", 55,"00564536"),
					new Person("Mike", "harvey", 21 ,"003685417"),
					new Person("gary", "hudson", 25,"00452341"))
					.collect(Collectors.toList());
		System.out.println("List of person objects :");
		personList.forEach(System.out::println);
		final String searchName = "mike";
		
		System.out.println("\nSearch objects having firstName = "+searchName);
		List<Person> foundObjs = personList.stream()
								 .filter(person -> 
								 		person.firstName.equalsIgnoreCase(searchName))
								 .collect(Collectors.toList());
		if(null != foundObjs) {
			foundObjs.forEach(System.out::println);
		} else {
			System.out.println("Could not found objects in list");
		}
		final int age = 21;
		System.out.println("\nSearch objects having firstName = "+searchName +
									"& age "+age);
		foundObjs = personList.stream()
								 .filter(person -> 
								 person.firstName.equalsIgnoreCase(searchName) 
								 	&&
								 person.age == age )
								 .collect(Collectors.toList());
		if(null != foundObjs) {
			foundObjs.forEach(System.out::println);
		} else {
			System.out.println("Could not found objects in list");
		}
	}
}

O/P- Filter POJO/user defined object from list of objects (lambda stream)

List of person objects :
[Mike harvey 34 001894536]
[Nick young 75 005425676]
[Jack slater 21 009654153]
[gary hudson 55 00564536]
[Mike harvey 21 003685417]
[gary hudson 25 00452341]

Search objects having firstName = mike
[Mike harvey 34 001894536]
[Mike harvey 21 003685417]

Search objects having firstName = mike& age 21
[Mike harvey 21 003685417]
Exit mobile version