Java 8 Stream - map() and collect() Example

In this tutorial, we will learn how to use Stream.map() and Stream().collect() method with an example.
Learn Stream API at https://www.javaguides.net/p/java-8-stream-api-tutorial.html.
The Stream.map() method is used to transform one object into another by applying a function.
The collect() method of Stream class can be used to accumulate elements of any Stream into a Collection.

Video Tutorial

This tutorial explained in below YouTube video:

User -> UserDTO Conversion using map() and collect() method

In this example, we create a User and UserDTO class and we use Stream.map() method to convert User into UserDTO class:
javapackage com.javaguides.java.tutorial;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StreamMapCollectExample {
    public static void main(String[] args) {

        List < User > users = new ArrayList < User > ();
        users.add(new User(1, "Ramesh", "secrete", "[email protected]"));
        users.add(new User(2, "Tony", "secrete", "[email protected]"));
        users.add(new User(3, "Tom", "secrete", "[email protected]"));

        List < UserDTO > usersDTO = new ArrayList < UserDTO > ();
        for (User user: users) {
            usersDTO.add(new UserDTO(user.getId(), user.getUserName(), user.getEmail()));
        }

        // using stream().map()

        List < UserDTO > usersDTOs = users.stream()
            .map((User user) -> new UserDTO(user.getId(), user.getUserName(), user.getEmail()))
            .collect(Collectors.toList());

        usersDTOs.forEach(System.out::println);

    }
}

class UserDTO {
    private int id;
    private String userName;
    private String email;

    public UserDTO(int id, String userName, String email) {
        super();
        this.id = id;
        this.userName = userName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ",email=" + email + "]";
    }
}

class User {
    private int id;
    private String userName;
    private String password;
    private String email;

    public User(int id, String userName, String password, String email) {
        super();
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email + "]";
    }
}
Output:
User [id=1, userName=Ramesh,[email protected]]
User [id=2, userName=Tony,[email protected]]
User [id=3, userName=Tom,[email protected]]

Comments