如何遍历HashMap的所有内容? 我只得到最后一次
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
// 0
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "name0");
map.put("company", "company0");
list.add(map);
// 1
map = new HashMap<String, String>();
map.put("name", "name1");
map.put("company", "company1");
list.add(map);
for (String key : map.keySet()) {
String value = map.get(key);
Log.w("dd:", "Key = " + key + ", Value = " + value);
}
我只是最后一次投放:
Key = company, Value = company1
Key = name, Value = name1
答案 0 :(得分:5)
列表中有多个地图。您需要遍历列表,然后遍历列表中的地图。
for(HashMap<String, String> curMap : list) {
//Put your loop over the map here.
}
答案 1 :(得分:0)
首先迭代列表,然后遍历地图。
for(Map<String, String> myMap: list){
for (Map.Entry<String, String> entry : myMap.entrySet()){
Log.w("dd:", "Key = " + entry.getKey() + ", Value = " +
entry.getValue());
}
}