From 8d7d98a144b2af7038efa1a266a284df92b9b7d9 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 26 Apr 2020 18:37:31 +0200 Subject: [PATCH] Customizing Java 8 code in modemmapper package --- .../com/baeldung/modelmapper/MapperUtil.java | 16 +++++++--------- .../baeldung/modelmapper/UserPropertyMap.java | 13 +++++++------ .../modelmapper/UsersListMappingUnitTest.java | 12 ++++++------ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index 60af44cc76..9c6a5a0320 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -2,11 +2,11 @@ package com.baeldung.modelmapper; import org.modelmapper.ModelMapper; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * This is a helper class that contains methods for generic mapping of the users list. + * This is a helper class that contains method for generic mapping of the users list. * Initially, an instance of ModelMapper was created. * * @author Sasa Milenkovic @@ -21,14 +21,12 @@ public class MapperUtil { } - public static List mapList(List sourceList, Class target) { - List targetList = new ArrayList(); + public static List mapList(List source, Class targetClass) { - for (S source : sourceList) { - targetList.add(modelMapper.map(source, target)); - } - - return targetList; + return source + .stream() + .map(element -> modelMapper.map(element, targetClass)) + .collect(Collectors.toList()); } } diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 0d2ebf7b4c..5b2942b158 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -4,11 +4,11 @@ import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. + * UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO. * In the configuration method, we call a converter to do the mapping. * * @author Sasa Milenkovic @@ -18,19 +18,20 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - protected List usernames; @Override protected List convert(List users) { - usernames = new ArrayList(); - users.forEach(user -> usernames.add(user.getUsername())); - return usernames; + return users + .stream() + .map(User::getUsername) + .collect(Collectors.toList()); } }; @Override protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); } } diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index aaeb8711a8..982622e1f5 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -59,7 +59,7 @@ public class UsersListMappingUnitTest { @Test public void givenUsersList_whenUseGenericType_thenMapToDto() { - // Mapping lists using generic type methods + // Mapping lists using custom type methods List userDtoList = MapperUtil.mapList(users, UserDTO.class); @@ -72,12 +72,12 @@ public class UsersListMappingUnitTest { UserList userList = new UserList(); userList.setUsers(users); - UserListDTO dto = new UserListDTO(); - modelMapper.map(userList, dto); + UserListDTO dtos = new UserListDTO(); + modelMapper.map(userList, dtos); - assertNotNull(dto); - assertThat(dto, Matchers.hasProperty("usernames")); - assertThat(dto.getUsernames(), hasSize(3)); + assertNotNull(dtos); + assertThat(dtos, Matchers.hasProperty("usernames")); + assertThat(dtos.getUsernames(), hasSize(3)); }