How to transform Array of objects to another Array of objects using Java 8 streams?

Hello everyone, here we will show you how to convert an Array of objects to another Array of objects 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.

Stream.of()

The Stream.of is used to create sequential streams for the given elements. We can pass a single element or multiple elements to Stream.of method.

Example program to convert an Array of User to Array of UserDto 

User.java

public class User {

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 User(String name,
String email, String phone) {

super();
this.name = name;
this.email = email;
this.phone = phone;
}
}


UserDto.java

public class UserDto {

private String name;
private String email;
private String phone;

public UserDto(String name,
String email, String phone) {
super();
this.name = name;
this.email = email;
this.phone = 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;
}
}

Mapper.java

import java.util.stream.Stream;

public class Mapper {

public UserDto[] map(User[] users) {

UserDto[] userDto = Stream.of(users).
map(o -> getDto(o)).toArray(UserDto[]::new);
return userDto;
}

private UserDto getDto(User user) {
return new UserDto(user.getName(),
user.getEmail(), user.getPhone());
}
}

Driver.java

public class Driver {

public static void main(String[] args) {

User user = new User("dummy",
"dummygmail@gmail.gmail", "+91-879");
User user1 = new User("dummy2",
"dummygmail@gmail.gmail2", "+91-8792");
User user3 = new User("dummy3",
"dummygmail@gmail.gmail3", "+91-87923");

User[] users = { user, user1, user3 };
Mapper mapper = new Mapper();
System.out.println(mapper.map(users)[0].getEmail());
System.out.println(mapper.map(users)[1].getEmail());
System.out.println(mapper.map(users)[2].getEmail());
}
}


Another example - Convert Array of Integer to Array of String

import java.util.stream.Stream;

public class Driver {

public static void main(String[] args) {

Integer[] numbers = { 3, 6, 4, 7, 2 };
String[] snumbers = Stream.of(numbers)
.map(o -> String.valueOf(o)).
toArray(String[]::new);
String num1 = snumbers[1];
System.out.println(num1);
}
}

Comments

Popular posts from this blog

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

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

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

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

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

Java - How to Count the Number of Occurrences of Substring in a String

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example