Mapping List to Map and vice versa

97 views
Skip to first unread message

Gin

unread,
May 13, 2018, 12:47:53 PM5/13/18
to modelmapper
Hi,


Is it possible to map:

public class Source {
   
List<ObjectA> data;
}

to

public class Dest {
   
Map<String, ObjectB> data;
}
?

I suspect this would take two steps, firstly converting the list to a map with value ObejctA and mapping both maps where ObejctA would be mapped to ObejctB.
Any hint how where to start with the modelMapper on this would be much appreciated.

Regards,
Gintas

Chun Han Hsiao

unread,
May 14, 2018, 9:19:06 PM5/14/18
to modelmapper
Hi Gintas,

We don't have predefined converter to convert a list to a map. But you can defined a custom converter for that.

Converter<List<ObjectA>, Map<String, ObjectB>> converter = new Converter<>() {
 
@Override
 
public Map<String, ObjectB> convert(Context<List<ObjectA>, Map<String, ObjectB>> ctx) {
   
// your code here
 
}

}
modelMapper
.addMappings(new PropertyMap<List<ObjectA>, Map<String, ObjectB>>() {
 
@Override
 
public void configure() {
   
using(converter).map().setData(source.getData());
 
}
});



Regards,
Chun Han

Gin於 2018年5月14日星期一 UTC+8上午12時47分53秒寫道:

Gintautas Sulskus

unread,
May 15, 2018, 5:07:42 AM5/15/18
to model...@googlegroups.com
Thank you, Chun. 

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages