arraylist里存放多个hashmap,如何读取
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("1", "aaa");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("物禅2", "bbb");
Map<敏毁String, Object> map3 = new HashMap<String, Object>();
map3.put("3", "ccc");
list.add(map1);
list.add(map2);
list.add(map3);
for (Map<String, Object>桥蚂备 map : list) {
for (Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
}
如果你的arrayList是下面这样的
List<Map<旅租String, Object>> listmap=new ArrayList<Map<String,Object>>();
取羡镇源值可以这样取,
for (HashMap<String, Object> eachmap : listmap) {
for (Entry<String, Object> entry : eachmap.entrySet()) {
System.out.println("兄态key:="+entry.getKey()+"; value:="+entry.getValue());
}
}
ps:手写的代码,试试把
arrayList里面存hashmap,袜链段唤举你也真够无聊的。
ArrayList<hashMap> list=new ArrayList<hashMap>();
添加元素省略告誉
取元素:
hashMap i=(hashMap)list.get(int index);
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("1", "yi"岁皮兄);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("2", "er");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("3", "san");
Map<String, String> map4 = new HashMap<握冲String, String>();
map4.put("4", "si");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
Iterator<Map<String, String>> iterator = list.iterator();
while (iterator.hasNext()) {
Map<String, String> map = iterator.next();
Set<String>乎袭 set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "***" + map.get(key));
}
}
for(int i=0;i<arraylist.size();i++){
Map map =(Map)arraylist.get(i);
}
这样燃盯好就可以皮铅啊则茄