Java Stream API - How to convert List of objects to another List of objects using Java streams?
Java Stream API
Stream map() Method
The Java 8 Stream map() is an intermediate operation. It converts Stream<obj1> to Stream<obj2>. For each object of type obj1, a new object of type obj2 is created and put in the new Stream. The map() operation takes a Function, which is called for each value in the input stream and produces one result value, which is sent to the output stream. The stream map method takes Function as an argument that is a functional interface.
Example 1- Convert List of User to List of UserDto
User.java
public class User {
private String id;
private String name;
private String email;
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User(String id, String name,
String email, String phone) {
super();
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id +
", name=" + name + ", email=" + email +
", phone=" + phone + "]";
}
}
UserDto.java
public class UserDto {
private String name;
private String email;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public UserDto(String name,
String email, String phone) {
super();
this.name = name;
this.email = email;
this.phone = phone;
}
@Override
public String toString() {
return "UserDto [name=" + name +
", email=" + email +
", phone=" + phone + "]";
}
}
public class UserDto {
private String name;
private String email;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public UserDto(String name,
String email, String phone) {
super();
this.name = name;
this.email = email;
this.phone = phone;
}
@Override
public String toString() {
return "UserDto [name=" + name +
", email=" + email +
", phone=" + phone + "]";
}
}
Mapper.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Mapper {
private List<User> users = new ArrayList<User>();
Mapper(List<User> users) {
this.users = users;
}
public List<UserDto> map() {
List<UserDto> userDto = users.stream().
map(o -> new UserDto(o.getName(),
o.getEmail(), o.getPhone()))
.collect(Collectors.toList());
return userDto;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Mapper {
private List<User> users = new ArrayList<User>();
Mapper(List<User> users) {
this.users = users;
}
public List<UserDto> map() {
List<UserDto> userDto = users.stream().
map(o -> new UserDto(o.getName(),
o.getEmail(), o.getPhone()))
.collect(Collectors.toList());
return userDto;
}
}
MainApplication.java
import java.util.ArrayList;
import java.util.List;
public class MainApplication {
public static void main(String[] args) {
User user = new User("1", "dummy",
"dummygmail@gmail.gmail", "!91-879");
User user1 = new User("2", "dummy2",
"dummygmail@gmail.gmail2", "!91-8792");
User user3 = new User("3", "dummy3",
"dummygmail@gmail.gmail3", "!91-87923");
List<User> users = new ArrayList<User>();
users.add(user3);
users.add(user1);
users.add(user);
Mapper mapper = new Mapper(users);
System.out.println(mapper.map());
}
}
Output:
Example 2 - Convert List of String to List of Integer in Java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Driver {
public static void main(String[] args)
{
//Convert List of String to List of Integer in Java
List<String> list = Arrays.asList
( "8" , "7", "36", "2" );
List<Integer> intList = list.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
System.out.println(intList);
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Driver {
public static void main(String[] args)
{
//Convert List of String to List of Integer in Java
List<String> list = Arrays.asList
( "8" , "7", "36", "2" );
List<Integer> intList = list.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
System.out.println(intList);
}
}
Console Output:
[8, 7, 36, 2]
Unlock the power of modern Java and transform your coding skills with
Modern Java in Action: Lambdas, Streams, Functional and Reactive Programming (2nd Edition)! 🌟
📚 Master the latest Java features with expert insights from
Raoul-Gabriel Urma, Alan Mycroft, and Mario Fusco. 🚀
💡 Elevate your programming game with Lambdas, Streams, Functional and Reactive programming techniques.
🔥 Unlock cleaner, more efficient code and embrace the future of Java!
🌐 Whether you're a seasoned developer or a curious learner, this book is your ultimate guide to mastering modern Java.
💥 Buy Now & Save 15%! 💥
Your Java journey begins here! ✨
Unlock the power of modern Java and transform your coding skills with
Modern Java in Action: Lambdas, Streams, Functional and Reactive Programming (2nd Edition)! 🌟
📚 Master the latest Java features with expert insights from
Raoul-Gabriel Urma, Alan Mycroft, and Mario Fusco. 🚀
💡 Elevate your programming game with Lambdas, Streams, Functional and Reactive programming techniques.
🔥 Unlock cleaner, more efficient code and embrace the future of Java!
🌐 Whether you're a seasoned developer or a curious learner, this book is your ultimate guide to mastering modern Java.
💥 Buy Now & Save 15%! 💥
Your Java journey begins here! ✨