Wednesday 5 July 2017

How to filter the List of Person using Java 8 Stream | Java 8 streams tutorial | Streams in Java 8


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=vApC1d-fuZ4&list=UUhwKlOVR041tngjerWxVccw

Person.java
public class Person
{
    private int id;
    private String name;
    private int age;

    public Person(int id, String name, int age)
    {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    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 "Person [id=" + id + ", name=" + name + ", age=" + age
                + "]";
    }

}
FilterDemo.java
import java.util.Arrays;
import java.util.List;

/**
 * 
 * Before JDK8 How to filter the personList?
 *
 */
public class FilterDemo
{
    public static void main(String[] args)
    {

        List<Person> personList = Arrays.asList(new Person(1, "Ram", 25),
                new Person(2, "Peter", 31), new Person(3, "Steve", 25),
                new Person(4, "Balu", 32));

        FilterDemo filterDemo = new FilterDemo();
        Person person = filterDemo.getStudentByName(personList, "Peter");
        System.out.println(person);
    }

    private Person getStudentByName(List<Person> personList, String name)
    {

        Person tempPerson = null;
        for (Person person : personList)
        {
            if (name.equals(person.getName()))
            {
                tempPerson = person;
            }
        }
        return tempPerson;
    }
}
Output
Person [id=2, name=Peter, age=31]
StreamDemo.java
import java.util.Arrays;
import java.util.List;
/**
 * 
 * With JDK8 Stream functionality How to filter the personList?
 *
 */
public class StreamDemo
{
    public static void main(String[] args)
    {

        List<Person> personList = Arrays.asList(new Person(1, "Ram", 25),
                new Person(2, "Peter", 31),
                new Person(3, "Steve", 25),
                new Person(4, "Balu", 32));


        Person person = personList.stream() //convert list to stream
                .filter(x -> "Peter".equals(x.getName())) //we want "Peter" only
                .findAny()  // If 'findAny' then return found
                .orElse(null);  //If not found, return null

        System.out.println(person);
    }

}
Output
Person [id=2, name=Peter, age=31]
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_PersonList_filter_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_PersonList_filter_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2094f8c76a9677f29e8569806a25a1b8b429324e/BasicJava/StreamDemo_PersonList_filter_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • 2 comments:

    1. This comment has been removed by the author.

      ReplyDelete
    2. Its fantatic explaintion lot of information gather it...nice article....
      seo company in Chennai

      ReplyDelete