public class LambdaMap {
private Map map = new HashMap<>();
@Before
public void initData() {
map.put(“key1”, “value1”);
map.put(“key2”, “value2”);
map.put(“key3”, “value3”);
map.put(“key4”, 4);
map.put(“key5”, 5);
map.put(“key5”, ‘h’);
}
/**
* 遍历Map的方式一
* 通过Map.keySet遍历key和value
*/
@Test
public void testErgodicWayOne() {
System.out.println(“———————Before JAVA8 ——————————“);
for (String key : map.keySet()) {
System.out.println(“map.get(” + key + “) = ” + map.get(key));
}
System.out.println(“———————JAVA8 ——————————“);
map.keySet().forEach(key -> System.out.println(“map.get(” + key + “) = ” + map.get(key)));
}
/**
* 遍历Map第二种
* 通过Map.entrySet