Files
java-tutorials/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java
T

38 lines
984 B
Java
Raw Normal View History

package com.baeldung.modelmapper;
2020-04-19 21:02:17 +02:00
import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.PropertyMap;
2020-04-21 22:26:22 +02:00
2020-04-19 21:02:17 +02:00
import java.util.List;
import java.util.stream.Collectors;
2020-04-19 21:02:17 +02:00
/**
* 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.
2020-04-21 22:26:22 +02:00
*
* @author Sasa Milenkovic
2020-04-19 21:02:17 +02:00
*/
public class UserPropertyMap extends PropertyMap<UserList, UserListDTO> {
2020-04-19 21:02:17 +02:00
Converter<List<User>, List<String>> converter = new AbstractConverter<List<User>, List<String>>() {
@Override
2020-04-19 21:02:17 +02:00
protected List<String> convert(List<User> users) {
return users
.stream()
.map(User::getUsername)
.collect(Collectors.toList());
2020-04-19 21:02:17 +02:00
}
};
@Override
protected void configure() {
2020-04-19 21:02:17 +02:00
using(converter).map(source.getUsers(), destination.getUsernames());
}
}