List<CustomerData> mapAddress(List<Address> addressList){
List<Customer> customerData = new ArrayList<Customer>();
if( CollectionUtils.isNotEmpty( addressList ) ){
for( Address address : addressList )
{
customerData.add( this.dozerBeanMapper.map( address, Customer.class ) );
}
}
return customerData;
}
CustomerData.java :
Has instance field 'address' of type String
Address.java
Has instance field 'mainLocation' of type String
Currently I am using for loop to map each object of Address with Customer, how can I directly map addressList with customerData (list to list) without looping. Can someone please help me with the xml file changes for this logic.
As far as I remember there was no possibility to map a Collection to a Collection in Dozer. You need to iterate over it. Take a look at this closed issue and the reason: https://github.com/DozerMapper/dozer/issues/5
What you could do to ease the pain would be using Java 8 (if you can) or Guava for some more declarative way of handling that mapping.
Java 8 example:
<FROM, TO> List<TO> mapList(List<FROM> fromList, final Class<TO> toClass) {
return fromList
.stream()
.map(from -> this.dozerBeanMapper.map(from, toClass))
.collect(Collectors.toList());
}
Guava example:
<FROM, TO> List<TO> mapList(List<FROM> fromList, final Class<TO> toClass) {
return Lists.transform(fromList, new Function<FROM, TO>() {
@Override
public TO apply(FROM from) {
return this.dozerBeanMapper(from, toClass);
}
});
}
Hi I think this could be useful, thanks @gmaslowski for the idea:
/**
* @param <T>
* @param <V>
* @param fromList list source objects
* @param toClass target class objects
* @param mapperId map-id in to dozer-mapping file
* @return list of toClass mapped
*/
public <T, V> List<V> mapList(List<T> fromList, Class<V> toClass, String mapperId) {
return fromList.stream().map(from -> {
try {
return this.dozerBean.getObject().map(from, toClass, mapperId);
} catch (Exception e) {
log.error("Error Dozer Mapping Objects {}", e.getMessage());
}
return null;
}).collect(Collectors.toList());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.