Victor
Victor

Reputation: 17077

BeanMap to HashMap conversion?

What is the easiest and most efficient way of converting a List<BeanMap> to a List <HashMap<String,String>>?

The BeanMap is based on an underlying POJO with 2 fields: 1 string, 1 date

Upvotes: 3

Views: 3924

Answers (1)

BalusC
BalusC

Reputation: 1108537

BeanMap implements Map<String, Object>, so you could just treat it as such.

List<BeanMap> beanMaps = getItSomehow();
List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

for (BeanMap beanMap : beanMaps) {
    Map<String, String> map = new HashMap<String, String>();

    for (Object o : beanMap.entrySet()) { // It's not parameterized :(
        Map.Entry<String, Object> entry = (Map.Entry<String, Object>) o;
        String key = entry.getKey();
        Object value = entry.getValue();
        map.put(key, value != null ? String.valueOf(value) : null);
    }

    maps.add(map);
}

// ...

Upvotes: 3

Related Questions