Java Stream API - How to convert List of objects to another List of objects using Java streams?

In this section, we will show you how to convert a List of objects to another List of objects in Java using the Java streams map(). The ‘map’ method maps each element to its corresponding result.


Java Stream API 

The Java Stream API provides a functional programming approach over the object oriented capabilities of Java. It is used for processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source List, Set or Array. Most of the stream operations return a Stream. This avails engender a chain of stream operations(stream pipe-lining). The streams withal support the aggregate or terminal operations on the elements. for example, finding the sum or maximum element or finding the average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. 

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 + "]";
}
}


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;

}
}


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);
}
}


Console Output:

[8, 7, 36, 2]

More interesting topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete