Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ModelMapper library support collections like ArrayList or HashSet?

This question is not relating with AutoMapper. My question is about ModelMapper in java, however I cannot create new tag for modelmapper as my little reputation. Sorry for confusion.

Anyway, my question is that does modelmapper library support collections like arraylist or hashset? it seems not support collection to collection mapping. Is it true?

like image 539
Ray Avatar asked Aug 26 '11 04:08

Ray


People also ask

What is the use of ModelMapper in Java?

ModelMapper, is an object-to-object framework that converts Java Beans (Pojos) from one representation to another. It automates different object mappings with a "convention follows configuration" approach allowing at the same time advanced functionality for cases with special needs.

What is spring boot ModelMapper?

The goal of ModelMapper is to make object mapping easy by automatically determining how one object model maps to another.


2 Answers

You can also map collections () directly:

    List<Person> persons = getPersons();     // Define the target type     java.lang.reflect.Type targetListType = new TypeToken<List<PersonDTO>>() {}.getType();     List<PersonDTO> personDTOs = mapper.map(persons, targetListType); 

Documentation on mapping Generics.

like image 149
José Avatar answered Sep 16 '22 14:09

José


Or with Java 8:

List<Target> targetList =     sourceList         .stream()         .map(source -> modelMapper.map(source, Target.class))         .collect(Collectors.toList()); 
like image 42
Phong Bui Avatar answered Sep 19 '22 14:09

Phong Bui