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<String, String> map = new HashMap<>();
map.put("1","2");
map.put("2","3");
map.put("3","4");
Map<String,String> map1 = Map.of("k1", "v1", "k2", "v2", "k3", "v3");//java9引入的方法,map1集合是不可变集合,且key和value的值都不能为null
Map<String,String> map2 = new LinkedHashMap<>(Map.of("k1", "v1", "k2", "v2", "k3", "v3"));
map2.put("1","2");
for (Iterator<Entry<String, String>> iter = map2.entrySet().iterator();iter.hasNext();) {
Entry<String, String> entry = iter.next();
System.out.println("key=" + entry.getKey());
System.out.println("value=" + entry.getValue());
}
System.out.println(map1.getClass());
}
}
使用entrySet遍历map
最新推荐文章于 2022-11-14 19:30:40 发布