-1

Let's say I have simple HashMap:

Map<String, String> map = new HashMap<>();
map.put("field1","value1");
map.put("field2", "value2");

I also have simple java class:

class SimpleClass {
 public String field1;
 public String field2;
}

What is simplest and most elegant way to create SimpleClass instance with corresponding fields/values taken from map? In this case, resulting SimpleClass instance should get field1 value 'value1andfield2valuevalue2`.

SimpleClass is already defined, now we need to find matching keys in map, if match found, it's value should be assigned to corresponding class field.

In my real application, I will get list of maps and I need to transform it into List<SimpleClass>. Map can contain additional keys, that need to be ommited (if no matching class field is available).

Can I use (for example) Guava to make transoformation like this? I'm on Android so java streams can't be used so far.

[edit]

My attempt:

 private SimpleClass mapToObject(Map<String, String> map)
    {
        SimpleClass result = new SimpleClass();

        for(Field f: result.getClass().getDeclaredFields())
        {
            try
            {
                f.setAccessible(true);
                f.set(result,map.get(f.getName()));
            }
            catch (Exception e)
            {
                Log.d("error", e.toString());
            }

        }
        return result;
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user1209216
  • 7,404
  • 12
  • 60
  • 123
  • 2
    Let's say you have written some code. You also have tried to execute it. What is the simplest and most elegant way you could demonstrate that you've actually attempted something? – Michael Apr 10 '19 at 11:16
  • Your comment is not very useful – user1209216 Apr 10 '19 at 11:16
  • Neither is your question, in its current state. – Michael Apr 10 '19 at 11:17
  • Really? Why are you saying that? – user1209216 Apr 10 '19 at 11:17
  • The absolute minimum that we expect is that you have at least attempted to solve the problem yourself. Why should we put in effort if you haven't? Maybe you have attempted something, but nothing about your questions suggests that you have. – Michael Apr 10 '19 at 11:21
  • The only way I think about is to use reflection to list class fields and construct class instance in loop, which is pretty bad solution in my opinion. – user1209216 Apr 10 '19 at 11:24
  • 1
    Mapping strings to field names (or method names, in the case of setters) is always going to involve reflection. Java is not a dynamic language, reflection is the only way to accomplish things like this. – Michael Apr 10 '19 at 11:25
  • You could loop your map, and [set the field value using reflection](https://stackoverflow.com/questions/24094871/set-field-value-with-reflection) – Leonardo Velozo Apr 10 '19 at 11:26
  • My attempt added, but I feel this is not best approach. – user1209216 Apr 10 '19 at 12:09

1 Answers1

1

Use ObjectMapper ,

SimpleClass  simpleClass = objectMapper.convertValue(map, SimpleClass.class);
Srinivasan Sekar
  • 2,049
  • 13
  • 22