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);
}
// ...
精彩评论