How to convert Set of objects to another Set of objects using Java streams?

Hello everyone, here we will show you how to convert a Set to another Set in Java using Java streams map(). The ‘map’ method maps each element to its corresponding result. 


Java Stream API

The Java Stream API provides a functional approach to processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source Collection or Array. Most of the stream operations return a Stream. This helps create a chain of stream operations(stream pipe-lining). The streams also support the aggregate or terminal operations on the elements. for example, finding the minimum or maximum element or finding 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. Stream map method takes Function as an argument that is a functional interface.

Convert Set of User to Set 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.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Mapper {

private Set<User> users = new HashSet<User>();

Mapper(Set<User> users) {
this.users = users;
}

public Set<UserDto> map() {

Set<UserDto> userDto = users.stream().
map(o -> new UserDto(o.getName(),
o.getEmail(), o.getPhone()))
.collect(Collectors.toSet());

return userDto;

}
}


Driver.java

import java.util.HashSet;
import java.util.Set;

public class Driver {

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

Set<User> users = new HashSet<User>();
users.add(user3);
users.add(user1);
users.add(user);

Mapper mapper = new Mapper(users);
System.out.println(mapper.map());
}
}


Output:



Another example - Convert Set of String to Integer Set

import java.util.Set;
import java.util.stream.Collectors;

public class Driver {

public static void main(String[] args)
{
//Convert Set of String to Integer Set in Java
Set<String> list = Set.of
( "8" , "7", "36", "2" );
Set<Integer> intList = list.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toSet());
System.out.println(intList);
}
}


Output:

[2, 36, 7, 8]

Popular posts from this blog

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

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

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

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