Customizing Java 8 code in modemmapper package

This commit is contained in:
Sasa M 2020-04-26 18:37:31 +02:00
parent 9f0d8cf6ab
commit c2e64bea72
1 changed files with 7 additions and 6 deletions

View File

@ -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<UserList, UserListDTO> {
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
protected List<String> usernames;
@Override
protected List<String> convert(List<User> users) {
usernames = new ArrayList<String>();
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());
}
}