- 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
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
<R>Stream <R> 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<Employee> employees = Arrays.asList( new Employee("Jai"), new Employee("Adithya"), new Employee("Raja")); List<String> 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]
- Related Articles
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to populate a Map using a lambda expression in Java?\n
- Convert 2D array to object using map or reduce in JavaScript
- Convert object to a Map - JavaScript
- Convert short primitive type to Short object in Java
- Convert byte primitive type to Byte object in Java
- How can we convert a map to the JSON object in Java?
- Java Program to convert integer to String with Map
- Convert JSON to/from Map using Jackson library in Java?
- How to convert a plain object into ES6 Map using JavaScript?
- Convert double primitive type to a Double object in Java
- How to check if an Image object intersects with another object using FabricJS?
- Convert a Map to JSON using the Gson library in Java?
- How to implement an instance method of an arbitrary object using method reference in Java?
- How to implement LongPredicate using lambda and method reference in Java?

Advertisements