Does ModelMapper library support collections like ArrayList or HashSet?
Asked Answered
W

5

31

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?

Weinstock answered 26/8, 2011 at 4:5 Comment(0)
G
63

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.

Gardening answered 29/7, 2013 at 8:41 Comment(0)
B
8

Or with Java 8:

List<Target> targetList =
    sourceList
        .stream()
        .map(source -> modelMapper.map(source, Target.class))
        .collect(Collectors.toList());
Brucite answered 22/3, 2019 at 15:22 Comment(4)
The disadvantage with this is that the modelMapper is not doing all of the mapping from one list to another. By using the TypeToken the mapping from one list to another is all encapsulated in the modelMapper.Yang
I think this way is more readable and I just simply hate that "{}.getType()" (it will look ugly if you reformat the code). And I said "Or..." though :DBrucite
I like this solution!Ground
elegant solutionApomict
R
5

You can also avoid the TypeToken stuff if you work with arrays:

  List<PropertyDefinition<?>> list = ngbaFactory.convertStandardDefinitions(props);
  ModelMapper modelMapper = new ModelMapper();
  PropertyDefinitionDto[] asArray = modelMapper.map(list, PropertyDefinitionDto[].class);
Rau answered 19/1, 2017 at 13:48 Comment(0)
R
4

Yes - Collection to Collection mapping is supported. Ex:

static class SList {
    List<Integer> name;
}

static class DList {
    List<String> name;
}

public void shouldMapListToListOfDifferentTypes() {
    SList list = new SList();
    list.name = Arrays.asList(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3));
    DList d = modelMapper.map(list, DList.class);

    assertEquals(d.name, Arrays.asList("1", "2", "3"));
}
Roath answered 25/7, 2012 at 16:55 Comment(2)
For this example you used two wrapper classes around the Collections. Would it be possible without them?Joscelin
@Joscelin - Yes, the wrappers just happened to be the example I gave.Roath
V
1

Even if all the answers are correct in their own way, I would like to share a rather simplified and easy way of doing it. For this example let's supose we have a list of entities from the database and we want to map into his respective DTO.

Collection<YourEntity> ListEntities = //GET LIST SOMEHOW;
Collection<YourDTO> ListDTO = Arrays.asList(modelMapper.map(ListEntities, YourDTO[].class));

You can read more at: https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html

You can still use a more old school way to do it: https://www.baeldung.com/java-modelmapper-lists

Use with moderation (or not).

Vankirk answered 10/4, 2021 at 20:0 Comment(2)
The resultant List will be sorted in what way? Since the target class is provided as an array (YourDTO[]) then converted to a List. Is there a way of knowing how the resultant list will be sorted?Sungod
@LewisMunene ListDTO should keep the same order as the ListEntities. #4972906Vankirk

© 2022 - 2024 — McMap. All rights reserved.