使用entrySet遍历map


package test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class test3 {
public static void main(String[] args) {
	Map map = new HashMap<>();
	map.put("1","2");
	map.put("2","3");
	map.put("3","4");
	Map map1 = Map.of("k1", "v1", "k2", "v2", "k3", "v3");//java9引入的方法,map1集合是不可变集合,且key和value的值都不能为null
	
	Map map2 = new LinkedHashMap<>(Map.of("k1", "v1", "k2", "v2", "k3", "v3"));
	map2.put("1","2");

	for (Iterator> iter = map2.entrySet().iterator();iter.hasNext();) {
		Entry entry = iter.next();
		System.out.println("key=" + entry.getKey());
		System.out.println("value=" + entry.getValue());
		
	}
	
	System.out.println(map1.getClass());
	
	
}
}


你可能感兴趣的:(使用entrySet遍历map)