Map的迴圈,基本的三種格式
*List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
Map<String,Object> map1 = new HashMap<String, Object>();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
List<String> list3 = new ArrayList<String>();
List<String> list4 = new ArrayList<String>();
list1.add("新增的Object-->a");
list2.add("新增的Object-->b");
list3.add("新增的Object-->c");
list4.add("新增的Object-->d");
map1.put("a",list1);
map1.put("b",list2);
map1.put("carno0", 123);
map1.put("name0", "liang");
list.add(map1);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("c",list3);
map2.put("d",list4);
map1.put("carno0", 123);
map1.put("name0", "liang");
list.add(map2);
System.out.println("========================第一種===========================");
for (Map<String,Object> map:list
) {
for (String s:map.keySet()
) {
System.out.print("key:"+s+"\t");
System.out.println("value:"+map.get(s));
}
}
System.out.println("=========================第二種==========================");
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
java.util.Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.print("key:"+str+"\t");
System.out.println("value:"+map.get(str));
}
}
System.out.println("===========================第三種=============================");
for (Map<String, Object> map : list) {
for (Map.Entry<String, Object> m : map.entrySet()) {
System.out.print("key:"+m.getKey() + " ");
System.out.println("value:"+m.getValue());
}
}
特點:
1)map的key採用簡單形式和複雜形式時,查詢的效率是不同的,簡單的key值效率更高
2)當資料量大的時候,採用entrySet遍歷key+value的效率要高於keySet
3)當我們只需要取得value值時,採用values來遍歷效率更高
相關部落格:https://blog.csdn.net/zhangsify/article/details/52966094
eg.
Map<String,Object> map0=new HashMap<String,Object>();
map0.put("userID", UserID);
List<Map<String,Object>> carmaps= new ArrayList<Map<String, Object>>();
carmaps= parkUserCarNoService.selectsysncarno(map0);
List<ParkUserCarNo> usercars=new ArrayList<ParkUserCarNo>();
int m1=0;StringBuffer sb=new StringBuffer();
for (Map<String,Object> map:carmaps
) {
// ParkUserCarNo usercarm1=new ParkUserCarNo();
int n1=0;
for (String s:map.keySet()
) {
++n1;
//usercarm1.setCarNO(carNO);
System.out.print("selectkey:"+s+"\t");
System.out.println("selectvalue:"+map.get(s));
if(s.equals("carNo")&&!Utils.isNull(map.get(s))){
String carNo="";
carNo=map.get(s).toString();
if(!Utils.isNull(carNo)){
if(m1==0){
sb.append(""+carNo+"");
}else{
sb.append("|"+""+carNo+"");
}
}
}
}
//usercars.add(usercarm1);
++m1;
}
System.out.println("獲取的車牌為:"+sb);