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()); } }