How to map collections in Dozer

前端 未结 8 740
悲&欢浪女
悲&欢浪女 2020-12-14 17:25

I\'d like to do something like:

ArrayList objects = new ArrayList();
...
DozerBeanMapper MAPPER = new DozerBeanMapper         


        
8条回答
  •  無奈伤痛
    2020-12-14 17:53

    I faced a similar issue, and decided on using a generic utility method to avoid iterating every time I needed to perform such mapping.

    public static  List map(final Mapper mapper, final List source, final Class destType) {
    
        final List dest = new ArrayList<>();
    
        for (T element : source) {
            dest.add(mapper.map(element, destType));
        }
    
        return dest;
    }
    

    Usage would then be something like:

        final List accounts..... 
        final List actual = Util.map(mapper, accounts, NewObject.class);
    

    Possibly this could be simplified further though.

提交回复
热议问题