Thursday 31 August 2017

How to convert list of sw engineer objects into a list of employee objects using Java 8 streams


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

Employee.java
public class Employee
{
    private String name;
    private int age;

    public Employee()
    {

    }

    public Employee(String name, int age)
    {
        super();
        this.name = name;
        this.age = 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 + "]";
    }

}
SoftwareEngineer.java
public class SoftwareEngineer
{
    private String name;
    private int age;

    public SoftwareEngineer(String name, int age)
    {
        super();
        this.name = name;
        this.age = 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;
    }

}
NonStreamDemo.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * List of objects -> List of other objects
 * 
 * This example shows you how to convert a list of SoftwareEngineer
 * objects into a list of Employee objects.
 */
public class NonStreamDemo
{
    public static void main(String[] args)
    {

        List<SoftwareEngineer> softwareEngineerList = Arrays.asList(
                new SoftwareEngineer("Ram", 30),
                new SoftwareEngineer("Peter", 27),
                new SoftwareEngineer("Dave", 33));

        // Before java 8
        List<Employee> employeeList = convertToEmployee(softwareEngineerList);
        System.out.println(employeeList);
    }

    private static List<Employee> convertToEmployee(List<SoftwareEngineer> softwareEngineerList)
    {

        List<Employee> employeeList = new ArrayList<>();

        for (SoftwareEngineer softwareEngineer : softwareEngineerList)
        {
            Employee employee = new Employee();
            employee.setName(softwareEngineer.getName());
            employee.setAge(softwareEngineer.getAge());
            employeeList.add(employee);
        }

        return employeeList;

    }

}
Output
[Employee [name=Ram, age=30], Employee [name=Peter, age=27], Employee [name=Dave, age=33]]

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

/**
 * 
 * List of objects -> List of other objects
 * 
 * This example shows you how to convert a list of SoftwareEngineer
 * objects into a list of Employee objects.
 */
public class StreamDemo
{
    public static void main(String[] args)
    {

        List<SoftwareEngineer> softwareEngineerList = Arrays.asList(
                new SoftwareEngineer("Ram", 30),
                new SoftwareEngineer("Peter", 27),
                new SoftwareEngineer("Dave", 33));

        // Java 8
        List<Employee> employeeList = softwareEngineerList.stream()
                .map(softwareEngineer -> {
                    Employee employee = new Employee();
                    employee.setName(softwareEngineer.getName());
                    employee.setAge(softwareEngineer.getAge());
                    return employee;
                    }).collect(Collectors.toList());

        System.out.println(employeeList);
    }

}
Output
[Employee [name=Ram, age=30], Employee [name=Peter, age=27], Employee [name=Dave, age=33]]

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_swl_to_empl_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_swl_to_empl_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
  • No comments:

    Post a Comment