Create New List from Existing List Using Lambda Expressions in Java



In this article, we will demonstrate how to create a new list of employee names from an existing list of Employee objects using Lambda Expressions. We will utilize Java's Stream API to efficiently map the employee data and collect it into a new list.

Lambda Expressions: Lambda expressions simplify functional programming by working with functional interfaces, which have only one method. A lambda expression provides a way to implement this method easily.

Steps

Following are the steps to create a new list with values from existing list with Lambda Expressions ?

  • Import the necessary classes from java.util package.
  • Define a list of Employee objects with name, age, and zone information.
  • Apply a Lambda function to extract the names from the employee list.
  • Utilize stream(), map(), and collect() methods to generate a new list of names.
  • Display the list of employee names.

Java Lambda: New list from existing list

Below is the Java program to create a new list with values from the existing list with Lambda Expressions ?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Demo {
    public static void main(String[] args) {
        List emp = new ArrayList<>(Arrays.asList(
                new Employee("Jack", 29, "South"),
                new Employee("Tom", 24, "North"),
                new Employee("Harry", 35, "West"),
                new Employee("Katie", 32, "East")
                                             ));

        Function getName = Employee::displayEmpName;
        List res = emp.stream()
                           .map(getName)
                           .collect(Collectors.toList());

        System.out.println("Employee Names = " + res);
    }
}

class Employee {
    private String emp_name;
    private int emp_age;
    private String emp_zone;

    public Employee(String emp_name, int emp_age, String emp_zone) {
        this.emp_name = emp_name;
        this.emp_age = emp_age;
        this.emp_zone = emp_zone;
    }

    public String displayEmpName() {
        return this.emp_name;
    }
}

Output

Employee Names = [Jack, Tom, Harry, Katie]

Code Explanation

The program begins by importing necessary classes such as ArrayList, Arrays, List, and Collectors from the java.util package and Function interface from the java.util.function package. The main class, Demo, initializes a list of Employee objects using Arrays.asList(). Each Employee object contains a name, age, and zone, with these attributes encapsulated within the Employee class. The map() method is then used with a Lambda expression (u -> u.displayEmpName()) to extract employee names. The collect(Collectors.toList()) method is employed to gather these names into a new list, res. Finally, the program prints the employee names. 

Updated on: 2024-08-12T23:13:48+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements