-1

I have a list of Car's, I want to map them to different specific Car's like - FordCar, HondaCar, VolkswagenCar... base on value of maker attribute of Car, that is Car.maker

Can Dozer, MapStruct do this? Or is there any framework in Java which can do this?

CR Sardar
  • 921
  • 2
  • 17
  • 32
  • 1
    Why Dozer? There is a standard Java type `Map`, see https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html – Honza Zidek Jul 01 '18 at 09:24
  • You can give MapStruct a try. http://mapstruct.org/ – Toan Lu Jul 01 '18 at 09:25
  • @ToanLu thanks for your quick response, but here my problem is little different, for example say - I have a list of `Car`s, I want to map them to `FordCar`, `HondaCar`, `VolkswagenCar`... base on value of `maker` attribute of `Car`, that is `Car.maker` – CR Sardar Jul 01 '18 at 09:40
  • @ToanLu: Yor example in the original question shows completely different case. Adjust your example to get better answers. – mentallurg Jul 01 '18 at 09:52
  • @mentallurg did you mean CS Sardar? – Toan Lu Jul 01 '18 at 10:32
  • the person who is giving -1 to the question, please be informed, i have done my home works, i have read entire documents of MapStruct, but still can not find anything helpful – CR Sardar Jul 03 '18 at 12:10

3 Answers3

0

If you don't have any extra requirement, you could do this simply using Java-8.

Map<String, Supplier<Car>> creators = new HashMap(){{
  put("honda", ()-> new HondaCar());
  put("ford", ()-> new FordCar());
}}

listOfCars.map(Car::getMaker).map(maker -> creators.get(maker).get())
Satyendra Kumar
  • 357
  • 2
  • 13
0

After long finding, conclusion - as of now MapStruct does not support it. That is mapping of Map<> entries. I did a round about - map a Map<> to a Map<> and flatten it using JackSon property @JsonAnyGetter and @JsonAnySetter

CR Sardar
  • 921
  • 2
  • 17
  • 32
-1

Orika, remap, Selma, MapStruct.

To get your specific mapping, implement it in the mapper.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
  • I have red example of those frameworks, but they are talking about `1-to-1` mapping, not `1-to-many` mapping, can you please read once my modified explanations – CR Sardar Jul 01 '18 at 10:02