对Map的几种遍历方式
代码:
package cn.hziee.graduation.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @Usage:
* @Author: hdy
* @Date: 2021/8/19
*/
public class Operation {
private final static Map<String, String> MY_MAP = new HashMap<>();
public static void main(String[] args) {
//往MY_MAP插入数据
MY_MAP.put("1", "2");
MY_MAP.put("2", "3");
MY_MAP.put("3", "4");
MY_MAP.put("a", "5");
System.out.println("-------------------keySet------------------");
Set<String> set = MY_MAP.keySet();
System.out.println(set);
System.out.println("-------------------keySet 迭代------------------");
//迭代器遍历Set
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
//获得key
String key = iterator.next();
String value = MY_MAP.get(key);
System.out.println("key: " + key + " value: " + value);
}
System.out.println("-------------------keySet->foreach------------------");
//foreach
set.forEach(key -> System.out.println("key: " + key + " value: " + MY_MAP.get(key)));
System.out.println("-------------------entrySet------------------");
//entrySet.
Set<Map.Entry<String, String>> entrySet = MY_MAP.entrySet();
System.out.println(entrySet);
System.out.println("-------------------entrySet 迭代------------------");
//iterator
Iterator<Map.Entry<String, String>> iterator1 = entrySet.iterator();
while (iterator1.hasNext()) {
Map.Entry<String, String> entry = iterator1.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key: " + key + " value: " + value);
}
System.out.println("-------------------entrySet->foreach------------------");
//entrySet->foreach
entrySet.forEach(entry -> System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()));
System.out.println("-------------------entrySet增强foreach------------------");
//增强foreach
for (Map.Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key: " + key + " value: " + value);
}
System.out.println("-------------------MY_MAP------------------");
System.out.println("-------------------MY_MAP->foreach------------------");
//MY_MAP->foreach
MY_MAP.forEach((key, value) -> {
System.out.println("key: " + key + " value: " + value);
});
}
}
结果:
-------------------keySet------------------
[1, a, 2, 3]
-------------------keySet 迭代------------------
key: 1 value: 2
key: a value: 5
key: 2 value: 3
key: 3 value: 4
-------------------keySet->foreach------------------
key: 1 value: 2
key: a value: 5
key: 2 value: 3
key: 3 value: 4
-------------------entrySet------------------
[1=2, a=5, 2=3, 3=4]
-------------------entrySet 迭代------------------
key: 1 value: 2
key: a value: 5
key: 2 value: 3
key: 3 value: 4
-------------------entrySet->foreach------------------
key: 1 value: 2
key: a value: 5
key: 2 value: 3
key: 3 value: 4
-------------------map------------------
-------------------map->foreach------------------
key: 1 value: 2
key: a value: 5
key: 2 value: 3
key: 3 value: 4