Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Convert an object to another type using map() method with Lambda in Java?
In Java 8, we have the ability to convert an object to another type using a map() method of Stream object with a lambda expression. The map() method is an intermediate operation in a stream object, so we need a terminal method to complete the stream.
Syntax
Stream map(Function super T,? extends R> mapper)
Example
import java.util.*;
import java.util.stream.*;
public class EmployeeInformationTest {
public static void main(String args[]) {
List employees = Arrays.asList(
new Employee("Jai"),
new Employee("Adithya"),
new Employee("Raja"));
List names = employees.stream()
.map(s -> s.getEmployeeName()) // Lambda Expression
.collect(Collectors.toList());
System.out.println(names);
}
}
// Employee class
class Employee {
private String empName;
private String empDesignation;
public Employee(String empName) {
this.empName = empName;
}
public String getEmployeeName() {
return empName;
}
public void setEmployeeName(String empName) {
this.empName = empName;
}
public String getEmployeeDesignation() {
return empDesignation;
}
public void setEmployeeDesignation(String empDesignation) {
this.empDesignation = empDesignation;
}
}
Output
[Jai, Adithya, Raja]
Advertisements
