hashmap 遍历

简介: 引用:http://apps.hi.baidu.com/share/detail/50038621 一。Java中遍历HashMap的两种方式第一种:  HashMap keySetMap = new HashMap();  Iterator keySetIterator = keySetMap.

引用:http://apps.hi.baidu.com/share/detail/50038621

一。Java中遍历HashMap的两种方式
第一种:
  HashMap<String,String> keySetMap = new HashMap<String,String>();
  Iterator<String> keySetIterator = keySetMap.keySet().iterator();
   while (keySetIterator.hasNext()) {
      System.out.println(keySetMap.get(keySetIterator.next()));
   }

第二种:
  HashMap<String,String> entrySetMap=new HashMap<String,String>();
  Iterator<Entry<String,String>> entrySetIterator=entrySetMap.entrySet().iterator();
   while(entrySetIterator.hasNext()){
      Entry<String,String> entry=entrySetIterator.next();
      System.out.println(entry.getValue());
   }
二。哪种方式更好?
HashMap的遍历有两种常用的方法,那就是使用keyset和entryset来进行遍历,但两者的遍历速度是有差别的,到底哪一种方式更好呢?测试一下就知道了。下面请看实例:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMapTest {
    public static void main(String[] args) {
        HashMap<String,String> keySetMap = new HashMap<String,String>();
        HashMap<String,String> entrySetMap=new HashMap<String,String>();
        
        for (int i= 0;i<1000;i++) {
            keySetMap.put(""+i, "keySet");
        }
        for(int i=0;i<1000;i++){
            entrySetMap.put(""+i,"entrySet");
        }
        
        long startTimeOne = System.currentTimeMillis();
        Iterator<String> keySetIterator = keySetMap.keySet().iterator();
        while (keySetIterator.hasNext()) {
            System.out.println(keySetMap.get(keySetIterator.next()));
        }
        System.out.println("keyset遍历时间:"+(System.currentTimeMillis()-startTimeOne));
        
        long startTimeTwo=System.currentTimeMillis();
        Iterator<Entry<String,String>> entrySetIterator=entrySetMap.entrySet().iterator();
        while(entrySetIterator.hasNext()){
            Entry<String,String> entry=entrySetIterator.next();
            System.out.println(entry.getValue());
        }
        System.out.println("entryset遍历时间:"+(System.currentTimeMillis()-startTimeTwo));
    }
}
通过多次运行测试发现,entryset遍历时间比keyset遍历时间短许多,entryset方式的性能通常要比keyset方式高一倍。
三。原因何在?
  通过查看源代码发现,调用keySetMap.keySet()这个方法会生成keyIterator迭代器,其next()方法只返回其key值,然后再通过key值在keySetMap中获得其value值,代码如:keySetMap.get(keySetIterator.next())
  而调用entrySetMap.entrySet()方法会生成EntryIterator迭代器,其next()方法返回一个Entry对象的一个实例,其中包含key值和value值。
  如果遍历HashMap时只取其key值,那么两种方式的遍历在性能上应该是相同的。但同时取key值和value值时,keyset方式比entryset方式多遍历了一次table,此时keyset方式性能差些。

相关文章
|
安全 Java API
java中HashMap的七种遍历方式
java.util.ConcurrentModificationException , 这种办法是非安全的 , 我们可以使用Iterator.remove() ,或者是Lambda 中的 removeIf() , 或者是Stream 中的 filter() 过滤或者删除相关数据
399 1
|
Java 数据库连接 API
HashMap 的 7 种遍历方式与性能分析!(强烈推荐)上
HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
503 0
HashMap 的 7 种遍历方式与性能分析!(强烈推荐)上
|
Java API
面试官上来就让手撕HashMap的7种遍历方式,当场愣住,最后只写出了3种
面试官上来就让手撕HashMap的7种遍历方式,当场愣住,最后只写出了3种
215 1
|
Java API
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!(2)
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!
369 0
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!(2)
|
Java 编译器
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!(1)
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!
316 0
公司新来一个同事:为什么 HashMap 不能一边遍历一边删除?一下子把我问懵了!(1)
|
安全 测试技术 API
HashMap 的 7 种遍历方式与性能分析!「修正篇」(下)
HashMap 的 7 种遍历方式与性能分析!「修正篇」
372 1
HashMap 的 7 种遍历方式与性能分析!「修正篇」(下)
|
存储 算法 安全
HashMap的遍历方式及底层原理
HashMap的遍历方式及底层原理
|
Java
【Java系列】HashMap的6种遍历方法
通过对map entrySet的遍历,也可以同时拿到key和value,一般情况下,性能上要优于上一种,这一种也是最常用的遍历方法。
486 0
【Java系列】HashMap的6种遍历方法
遍历HashMap的四种方式
遍历HashMap的四种方式
262 0