Customizing Java 8 code in modemmapper package
This commit is contained in:
+7
-9
@@ -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 <S, T> List<T> mapList(List<S> sourceList, Class<T> target) {
|
||||
List<T> targetList = new ArrayList<T>();
|
||||
public static <S, T> List<T> mapList(List<S> source, Class<T> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-6
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user