开发者

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


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);
}

// ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜