- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to create a new list with values from existing list with Lambda Expressions
To create a new list with values from existing list with Lambda Expressions, the following is an example.
Here, we are displaying the name of Employees. Therefore, we have created an Employee class as well −
List<Employee>emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"), new Employee("Harry", 35, "West"),new Employee("Katie", 32, "East"));
Use Lambda to crate anew list from existing list with Lambda Expressions −
List<String>res = emp.stream().map(u ->u.displayEmpName()).collect(Collectors.toList());
Let us see the complete example −
Example
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<Employee>emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"), new Employee("Harry", 35, "West"),new Employee("Katie", 32, "East")); List<String>res = emp.stream().map(u ->u.displayEmpName()).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]
- Related Articles
- Java Program to create a new list with values from existing list with Function Mapper
- Java Program to initialize a HashMap with Lambda Expressions
- How do you create a list with values in Java?
- Can we sort a list with Lambda in Java?
- Create new linked list from two given linked list with greater element at each node in C++ Program
- C# program to create a List with elements from an array
- Java program to print unique values from a list
- Python - Create a dictionary using list with none values
- Program to iterate over a List using Java 8 Lambda
- Java Program to remove an element from List with ListIterator
- Java Program to get the reverse of an Integer array with Lambda Expressions
- Program to create a list with n elements from 1 to n in Python
- Python program to mask a list using values from another list
- Create a to-do list with JavaScript
- How to create a thread using lambda expressions in Java?\n

Advertisements