遍历HashMap的四种方式

Posted bkxk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了遍历HashMap的四种方式相关的知识,希望对你有一定的参考价值。

public static void main(String[] args)


Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");

//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet())
System.out.println("key= "+ key + " and value= " + map.get(key));


//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext())
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());


//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet())
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values())
System.out.println("value= " + v);


   Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项(里面有Key和Value),而Set<Map.Entry<K,V>>表示一个映射项的Set。Map.Entry里有相应的getKey和getValue方法,即JavaBean,让我们能够从一个项中取出Key和Value。
---------------------

以上是关于遍历HashMap的四种方式的主要内容,如果未能解决你的问题,请参考以下文章

遍历HashMap的四种方法

遍历HashMap的四种方法

HashMap的四种遍历方法,及效率比较(简单明了)

Map集合的四种遍历方式

map的四种遍历方式

HashMap循环遍历方式及其性能对比