keySet和entrySet效率比较

Map<String,String> map=new HashMap<String, String>();
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
map.put("5","e");
map.put("6","f");
map.put("7","g");
map.put("8","h");
map.put("9","j");
Set<String> ls=map.keySet();//这里是生成键的视图  如果要读取还必须get一次
for(String l:ls)
System.out.println("k="+l+" v="+map.get(l));
Set<Map.Entry<String, String>> lv=map.entrySet();//这里是生成键和映射关系的视图    不需要再get一次。所以效率明显快
for(Map.Entry<String, String> l:lv)
System.out.println(l.getKey()+"-----"+l.getValue());
}

你可能感兴趣的:(keyset,entrySet)