java中遍历MAP的几种方法
java中遍历MAP的几种方法
Java代码
Map<String,String> map=new HashMap<String,String>();
map.put("username", "qq");
map.put("passWord", "123");
map.put("userID", "1");
map.put("email", "qq@qq.com");
Map<String,String> map=new HashMap<String,String>();
map.put("username", "qq");
map.put("passWord", "123");
map.put("userID", "1");
map.put("email", "qq@qq.com");
第一种用for循环
Java代码
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代
Java代码
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
用keySet()迭代
Java代码
Iterator it=map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=map.get(key);
System.out.println(key+"--"+value);
}
Iterator it=map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=map.get(key);
System.out.println(key+"--"+value);
}
用entrySet()迭代
Java代码
Iterator it=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}
上述是转载网址: http://rain-2372.iteye.com/blog/1615615
下述代码是自己增加:
System.out.println("==================================================");
// 定义Map
Map<String, Object> mapTestValue1 = new HashMap<String , Object>();
Map<String, Object> mapTestValue2 = new HashMap<String , Object>();
Map<String, Object> mapTestValue3 = new HashMap<String , Object>();
mapTestValue1.put("111", "1111");
mapTestValue2.put("222", "2222");
mapTestValue3.put("333", "3333");
// 定义Map 嵌套Map<String , Object> 对象
// 第一种定义方式
Map<String , Map<String, Object>> mapObject = FastMap.newInstance();
// 第二种定义方式
Map<String , Map<String, Object>> mapObject2 = new HashMap<String, Map<String, Object>>() ;
// 将Map 作为Value 加载到Map对象中
mapObject.put("11", mapTestValue1);
mapObject.put("22", mapTestValue2);
mapObject.put("33", mapTestValue3);
// 遍历map数据 使用Set格式进行接收
Set<Entry<String, Map<String, Object>>> setMap = mapObject.entrySet();
for (Entry<String , Map<String , Object>> setEntry : setMap) {
String key = setEntry.getKey() ;
System.out.println("setEntry.getKey() : " + setEntry.getKey() + " , setEntry.getValue() : " + setEntry.getValue() );
Map<String , Object> mapKeyValue = setEntry.getValue() ;
// System.out.println("mapKeyValue.getKey() : " + mapKeyValue. + " , mapKeyValue.getValue() : " + mapKeyValue.getValue() );
System.out.println("111 : " + mapKeyValue.get("111"));
System.out.println("222 : " + mapKeyValue.get("222"));
System.out.println("333 : " + mapKeyValue.get("333"));
}