0

I have map populated from database Data. I need to get values from there to Object. Field names in POJO and key names in Map object are different. I did it as the below. is there any effective way to do this

 Map<String ,Object> map; //retrieved from database Employee e = new Employee(); if(map!=null) { if(map.containsKey("name")) { e.setFirstName(map.get("name")); } if(map.containsKey("ads")) { e.setMyAddress(map.get("ads")); } if(map.containsKey("country")) { e.setDealCountry(map.get("country")); } if(map.containsKey("keyId")) { e.Id(map.get("keyId")); } } public class Employee { String firstName; String id; String myAdreess; String dealCountry; //setter getters } 
5
  • There are other ways to do it, but in all likelihood they will not be better than doing it this way. I’m sure you’ll get at least one reflection-based answer, but reflection would be a terrible way to do it, as it’s prone to errors which will be pretty hard to detect. Commented Feb 26, 2020 at 15:06
  • I am not quite sure what your Problem is exactly?! I haven't tested your code but what is the issue? Are you just asking if this is a valid/effective way to do whatever you are doing? Commented Feb 26, 2020 at 15:08
  • I understand that you think this is ugly and I would look for alternatives as well. As long as you don't have to do this for countless classes, your approach is fine. Take a look at my answer below for some other magic. Commented Feb 26, 2020 at 15:15
  • @MichaelHolley Yes i Need to know whether there is any efficient way to do this. Because i have to write lot of if conditions for multiple field checking Commented Feb 26, 2020 at 16:44
  • 1
    Just remove the if(map.containsKey(…)) checks. If your Employee has no meaningful default values for the properties anyway, the worst thing that can happen with e.setFirstName(map.get("name")); without a check, is that it sets a null property to null again. Otherwise, if you have meaningful defaults, just use them, e.g. e.setFirstName(map.getOrDefault("name", "no name in db stored")); Commented Feb 26, 2020 at 17:27

2 Answers 2

1

You can use java8+'s BiConsumer to define a setter for each of the map's keys:

 Map<String, BiConsumer<Employee, String>> consumerMap = new HashMap<>(); consumerMap.put("name", Employee::setFirstName); consumerMap.put("ads", Employee::setMyAddress); consumerMap.put("country", Employee::setDealCountry); //and so on Map<String, String> map = new HashMap<>();//the data from your database Employee e = new Employee(); if (map != null) { consumerMap.forEach((key, value) -> { if (map.containsKey(key)) { //value is the BiConsumer value.accept(e, map.get(key)); } }); } 

The forEach iterates over all the the defined mappings, then checks if your Map from the database has the respective keys and applies the data.

This does not involve any (manual) reflection and the mapping from the map's key to the setter is very direct.

Sign up to request clarification or add additional context in comments.

1 Comment

Or map.forEach((key,value) -> consumerMap.getOrDefault(key, (x,y)->{}).accept(e, value));
0

You can take a look to Hibernate or other ORM's. They automatically do the mapping for you.

Here for example a link to the Hibernate ORM: https://hibernate.org/

It's easier and faster than make the mapping manuelly. ;)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.