map.containsKey(key):判断map是否包含你指定的key名
Map map = new HahsMap();
map.put("yangrong","123");
//判断map对象中的键是否包含了yangrong这个键
if(map.containsKey("yangrong")) {
.........
}
=======================================================================
//判断Map是否为空
Map clientExtInfoMap = getClientExtInfo(clientId);
//isEmpty()方法也是判断Map是否为空
if (clientExtInfoMap == null || clientExtInfoMap.isEmpty()) {
return false;
}
====================================================================
Map对象遍历方法:
Map map = readOutXMl(content);
//遍历Map
/* Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
System.out.println("key="+key+",value="+value);
}
方法2:
Set set = map.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
String key = (String) iterator.next(); //获取键
String value = (String)map.get(key); //通过键获取值
System.out.println("key="+key+",value="+value);
}
方法3:
Map map = new HashMap();
map.put("getParoduct","");
for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
String key = (String) it.next(); //获取Map中的键
String value = (String)map.get(key); //通过键获取值
System.out.println("键="+ key +" ,值=" +value);
}
===================================================================
一个Map对象中封装了List容器,然后在List容器列表中又封装了map对象:
Map map = new HashMap();
map.put("month","201012");
map.put("custName","杨荣");
map.put("requestId","20101210091213");
Map map2 = new HashMap();
map2.put("productNo","18979177626");
map2.put("payId","162414695");
map2.put("cityCode","0791");
Map map3 = new HashMap();
map3.put("productNo","18979101937");
map3.put("payId","163414690");
map3.put("cityCode","0792");
List list = new ArrayList();
list.add(map2); //把Map对象装到List容器中
list.add(map3);
map.put("rechargeList", list); //再把List放到map对象中
2.怎么进行取值:
List list = new ArrayList();
list = (List)map.get("rechargeList"); //map对象是上面中设置的对象
for(int i=0;i <list.size();i++){
Map map2 = (Map)list.get(i); //转为Map对象
String productNo = map2.get("productNo").toString();
String payId = map2.get("payId").toString();
String cityCode = map2.get("cityCode").toString();
}
================================================================================
注意:
【在一个List对象中存的是一个Map对象,且MaP对象中的键是string类型,
值又是一个Map对象类型】
解析:
//List对象中封装Map对象,且Map对象中的值又是Map对象
List<Map> list = new ArrayList<Map>();
Map map = new HashMap();
map.put("lanCode", "0791");
map.put("productNo", "18979110001");
Map map2 = new HashMap();
map2.put("yangrong", map); //这个Map对象中的值又是一个Map对象
list.add(map2); //这里又把Map对象放到List中
List<Map> list2 = (List)list; //这里我先用一个List来接做为例子讲
//采用增强for循环
for (Map<String, Map> ee : list2) {
//判断ee这个Map对象中是否包含了yangrong这个键
if(ee.containsKey("yangrong")) {
//解析
Map map3 = (Map) ee.get("yangrong"); //根据键是yangrong的去获取值,且值是要转换为一个Map对象
for (Iterator<String> it = map3.keySet().iterator(); it.hasNext();) {
String keys = (String) it.next(); //获取键为yangrong对应的值的Map对象中的键
System.out.println("Map对象中值对应Map的keys="+keys +",map的值="+map3.get(keys));
}
}
}
==============================================================================================
4:拿到List对象中封装的Map对象的值:
List list = NewCRM_Util.getMapXml(outXml); //getMapXml()返回的是List列表
for(int i = 0; i < list.size(); i++) {
Map map = (Map)list.get(i);
System.out.println("resultCode=:"+map.get("resultCode").toString); //resultCode表示Map对象的键
} |