Java, Map same object to different objects

Multi tool use
Multi tool use


Java, Map same object to different objects



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


Car


Car


FordCar


HondaCar


VolkswagenCar


maker


Car


Car.maker



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





Why Dozer? There is a standard Java type Map, see docs.oracle.com/javase/tutorial/collections/interfaces/map.html
– Honza Zidek
Jul 1 at 9:24


Map





You can give MapStruct a try. mapstruct.org
– Toan Lu
Jul 1 at 9:25





@ToanLu thanks for your quick response, but here my problem is little different, for example say - I have a list of Cars, I want to map them to FordCar, HondaCar, VolkswagenCar... base on value of maker attribute of Car, that is Car.maker
– CR Sardar
Jul 1 at 9:40


Car


FordCar


HondaCar


VolkswagenCar


maker


Car


Car.maker





@ToanLu: Yor example in the original question shows completely different case. Adjust your example to get better answers.
– mentallurg
Jul 1 at 9:52





@mentallurg did you mean CS Sardar?
– Toan Lu
Jul 1 at 10:32




2 Answers
2



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())



Orika, remap, Selma, MapStruct.



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





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 1 at 10:02


1-to-1


1-to-many






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

rw T8wTAz5cy
PFU bkZyq10bheqwW vHfoxKpb2r,0UURpCc6ns,kOx3odJGKy8vEMVdb8u,YpQ3ZA5Vz,2USnEY,wu IfJdADxGnEclWpIOIVIwE9

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters