Java

Mapping Lists of different elements types using Model Mapper

Guide on Mapping List of different element types. Covert list of a Java Beans into a List of another Java Bean using Model Mapper Library. Read this article if you want to convert List<DTO> to a List<Entity> and vice versa.

Overview

This tutorial explains how to use ModelMapper library to map lists of different types. In other words, covert a list of one type of elements into a list of different type of elements. In a typical web based applications we keep Entities and DTOs separated. However, they mostly have similar fields. The take away from this tutorial will also help you to convert a List<Entity> to List<DTO> and vice versa.

This tutorial is limited to converting between lists of different element types. If your are looking for Mapping an Entity Bean to a DTO and vice versa, please Convert Entity To DTO In Spring REST API.

First, we will setup ModelMapper library into the application.
Add Model Mapper dependency to your project. As a best practice, as always use the latest version of libraries.

<dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>{version}</version> </dependency>
Code language: HTML, XML (xml)

Mapping Lists using Model Mapper

Next are the two Java Beans that we want to map.

First Java bean is a StudentDto class that is a Data Transfer Object in the application.

@AllArgsConstructor @NoArgsConstructor public class StudentDto { private String studentId; private String firstName; private String lastName; private int year; }
Code language: Java (java)

Next Java bean is a Student class, which can also be an entity bean in a typical application.

@AllArgsConstructor public class Student { private String studentId; private String firstName; private String lastName; private int year; }
Code language: Java (java)

Now, we will create a List of Student Entity Bean and map it to a List of Student DTO objects using model mapper.

Example of mapping List<Entity> to List<DTO> using model mapper.

List<Student> students = List.of( new Student("123", "Baristan", "Selmy", 2024), new Student("125", "Strong", "Belwas", 2029) ); ModelMapper mapper = new ModelMapper(); TypeToken<List<StudentDto>> typeToken = new TypeToken<>() { }; List<StudentDto> studentDtos = mapper.map(students, typeToken.getType());
Code language: Java (java)

First, we created a List of Entity Beans with two entities. Next, we created a Model Mapper Instance.

The map(), method on the modelMapper takes the source object and type of the destination object. In our case the type of the destination object uses generic type. Thus, we created an anonymous instance of TypeToken by providing the correct generic type argument. Next, we use the type token to perform the mapping.

Finally, we executed the map() method by providing the source list and the type of the type token and that returns the List of Student DTO objects.

Validate Before Mapping

In the previous example, the source bean and the destination beans had exactly same fields with same data types. But, sometimes the fields may contain totally different fields. Thus, the mapping may fail with an exception.

Using ModelMapper, we can validate the compatibility of both of the beans using validate() method.

ModelMapper mapper = new ModelMapper(); mapper.createTypeMap(StudentDto.class, Student.class); mapper.validate();
Code language: Java (java)

The validate() method throws ValidationException, if the two beans are not compatible to each other for the mapping purpose.

Mapping Lists Iteratively

Instead of using TypeToken and asking Model mapper to map lists of different types, we can also iterate through the list and map individual beans.

List<StudentDto> dtos = new ArrayList<>(); ModelMapper mapper = new ModelMapper(); students.forEach(student -> dtos.add(mapper.map(student, StudentDto.class)));
Code language: Java (java)

Note that, we do not have a generic type here. Thus, we can provide destination object type explicitly without using TypeToken.

Summary

In this tutorial we learned to map Lists of different element types using Model Mapper library. The Model Mapper library can smartly identify mapping fields between the two element types. It Also provides ways to customise some of its mapping behaviours. For more on Java, please visit Java Tutorials.