大家好,又见面了,我是你们的朋友全栈君。
遍历方法一( entrySet() 遍历):
Iterator it = tempMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
遍历方法二( entrySet() 遍历):
for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
}
遍历方法三( keySet() 遍历):
for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循环输出key
System.out.println("key=" + obj + " value=" + tempMap.get(obj));
}
for (Iterator i = tempMap.values().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循环输出value
}
遍历方法四( keySet() 遍历):
for (Object o : tempMap.keySet()) {
System.out.println("key=" + o + " value=" + tempMap.get(o));
}
遍历方法五(遍历Map <String, ArrayList> map = new HashMap<String, ArrayList>();):
Map<String, ArrayList> map = new HashMap<String, ArrayList>();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
ArrayList arrayList = map.get(key);
for (Object o : arrayList) {
System.out.println(o + "遍历过程");
}
}
or
Map<String, List> mapList = new HashMap<String, List>();
for (Map.Entry entry : mapList.entrySet()) {
String key = entry.getKey().toString();
List<String> values = (List) entry.getValue();
for (String value : values) {
System.out.println(key + " --> " + value);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128945.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...