HashMap的2种遍历方法

本文介绍两种HashMap遍历方法:使用keySet与entrySet,并通过实验对比两者效率。结果显示,使用entrySet遍历效率更高,因为它将key和value一同存储于entry中。

第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry entry = (Map.Entry) iter.next();
    Object key = entry.getKey();
    Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
    Object key = iter.next();
    Object val = map.get(key);
}
效率低,以后尽量少使用!

例:
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:

public class HashMapTest {
public static void main(String[] args) ...{
  HashMap hashmap = new HashMap();
  for (int i = 0; i < 1000; i ) ...{
   hashmap.put("" i, "thanks");
  }

  long bs = Calendar.getInstance().getTimeInMillis();
  Iterator iterator = hashmap.keySet().iterator();  
  while (iterator.hasNext()) ...{   
   System.out.print(hashmap.get(iterator.next()));
  }
  System.out.println();
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
  listHashMap();
}

  public static void listHashMap() ...{
  java.util.HashMap hashmap = new java.util.HashMap();
  for (int i = 0; i < 1000; i ) ...{
   hashmap.put("" i, "thanks");
  }
  long bs = Calendar.getInstance().getTimeInMillis();  
  java.util.Iterator it = hashmap.entrySet().iterator();
  while (it.hasNext()) ...{
   java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
   // entry.getKey() 返回与此项对应的键
   // entry.getValue() 返回与此项对应的值
   System.out.print(entry.getValue());
  }
  System.out.println();
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}

对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。

在Java中,可以使用以下三种方法遍历HashMap: 1. 使用Iterator遍历HashMap 使用Iterator迭代器遍历HashMap,可以通过调用HashMap的entrySet()方法获取到HashMap中所有键值对的Set集合,然后通过迭代器Iterator遍历该Set集合,最后取出每个键值对的key和value。示例代码如下: ``` HashMap<String, Integer> hashMap = new HashMap<>(); // 添加键值对 hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); // 遍历HashMap Iterator<Map.Entry<String, Integer>> iterator = hashMap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String, Integer> entry = iterator.next(); String key = entry.getKey(); Integer value = entry.getValue(); System.out.println("key: " + key + ", value: " + value); } ``` 2. 使用foreach遍历HashMap的键或值 使用foreach循环遍历HashMap的键或值,可以通过调用HashMap的keySet()方法获取到所有的键的Set集合,或者通过调用values()方法获取到所有的值的Collection集合,然后通过foreach循环遍历即可。示例代码如下: ``` HashMap<String, Integer> hashMap = new HashMap<>(); // 添加键值对 hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); // 遍历HashMap的键 for(String key : hashMap.keySet()) { System.out.println("key: " + key); } // 遍历HashMap的值 for(Integer value : hashMap.values()) { System.out.println("value: " + value); } ``` 3. 使用Lambda表达式遍历HashMap 使用Lambda表达式遍历HashMap,可以直接通过调用HashMap的forEach()方法,传入一个Lambda表达式,即可遍历HashMap的键值对。示例代码如下: ``` HashMap<String, Integer> hashMap = new HashMap<>(); // 添加键值对 hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); // 遍历HashMap hashMap.forEach((key, value) -> System.out.println("key: " + key + ", value: " + value)); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值