java遍历Map的4种方法及效率对比

一、Map遍历的方式

先创建一个map,添加好数据:

Map map=new HashMap();
for(int i=0;i<10000;i++){
	map.put(i, "a"+i);
}

1、keySet的for循环方式:

//只获取key
public static void keySetForGetKey(Map map){
    long startTime = System.currentTimeMillis();
    for (String key : map.keySet()) {
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetForGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void keySetForGetKeyAndValue(Map map){
    long startTime = System.currentTimeMillis();
    for (String key : map.keySet()) {
        String value = map.get(key);
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetForGetKeyAndValue运行时间" + (endTime - startTime));
}

2、keySet的iterator迭代器方式:

//只获取key
public static void keySetIteratorGetKey(Map map){
    long startTime = System.currentTimeMillis();
    Iterator iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetIteratorGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void keySetIteratorGetKeyAndValue(Map map){
    long startTime = System.currentTimeMillis();
    Iterator iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        String value = map.get(iterator.next());
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetIteratorGetKeyAndValue运行时间" + (endTime - startTime));
}

3、entrySet的for循环方式【同时获取key、value】:

/只获取key
public static void entrySetForGetKey(Map map){
    long startTime = System.currentTimeMillis();
    for (Entry entry : map.entrySet()) {
        String key = entry.getKey();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetForGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void entrySetForGetKeyAndValue(Map map){
    long startTime = System.currentTimeMillis();
    for (Entry entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetForGetKeyAndValue运行时间" + (endTime - startTime));
}

4、entrySet的iterator迭代器方式【同时获取key、value】:

//只获取key
public static void entrySetIteratorGetKey(Map map){
    long startTime = System.currentTimeMillis();
    Iterator> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next().getKey();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetIteratorGetKey运行时间" + (endTime - startTime));
}
//获取key和value
public static void entrySetIteratorGetKeyAndValue(Map map){
    long startTime = System.currentTimeMillis();
    Iterator> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next().getKey();
        String value = iterator.next().getValue();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetIteratorGetKeyAndValue运行时间" + (endTime - startTime));
}

二、总结效率

entrySet的方式整体都是比keySet方式要高一些;
单纯的获取key来说,两者的差别并不大,但是如果要获取value,还是entrySet的效率会更好,因为keySet需要从map中再次根据key获取value,而entrySet一次都全部获取出来
iterator的迭代器方式比foreach的效率高。


--------------------- 
作者:zajiayouzai 
来源:CSDN 
原文:https://blog.csdn.net/zajiayouzai/article/details/80922610 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Java,SE)