zoukankan      html  css  js  c++  java
  • Map 遍历的几种方式和性能分析

    Map遍历方式主要有entrySet和keySet,还可以用迭代器 Iterator

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    public class Demo {

    public static void main(String[] args){
    Map<String, Object> map = getMap();
    keySet(map);
    keySetIterator(map);
    entrySet(map);
    entrySetIterator(map);
    java8(map);
    }

    public static Map getMap(){
    Map<String, Object> map = new HashMap<String, Object>();
    for(int i = 0;i<1000000; i++){
    map.put("A"+i,i);
    }
    return map;
    }
    public static void keySet(Map<String,Object> map){
    long startTime = DateUtil.currentTimeMilliSeconds();
    int count = 0;
    for(String key : map.keySet()){
    count++;
    String value = map.get(key).toString();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetGetKeyAndValue运行时间" + (endTime - startTime)+"----"+count);
    }
    public static void keySetIterator(Map<String,Object> map){
    long startTime = DateUtil.currentTimeMilliSeconds();
    Iterator<String> iterator = map.keySet().iterator();
    int count = 0;
    while(iterator.hasNext()){
    count++;
    String key = iterator.next();
    String value = map.get(key).toString();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("keySetIteratorGetKeyAndValue运行时间" + (endTime - startTime)+"---------"+count);
    }
    public static void entrySet(Map<String,Object> map){
    long startTime = DateUtil.currentTimeMilliSeconds();
    int count = 0;
    for(Map.Entry<String,Object> entry: map.entrySet()){
    count++;
    String key = entry.getKey();
    String value = entry.getValue().toString();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetGetKeyAndValue运行时间" + (endTime - startTime)+"-----"+count);
    }
    public static void entrySetIterator(Map<String,Object> map){
    long startTime = DateUtil.currentTimeMilliSeconds();
    Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
    int count = 0;
    while (iterator.hasNext()) {
    count++;
    Map.Entry<String,Object> entry = iterator.next();
    String key = entry.getKey();
    String value = entry.getValue().toString();
    }
    long endTime = System.currentTimeMillis();
    System.out.println("entrySetIteratorGetKeyAndValue运行时间" + (endTime - startTime)+"---"+count);
    }
    public static void java8(Map<String,Object> map){
    long startTime = DateUtil.currentTimeMilliSeconds();
    map.forEach((key, value) -> {
    // String key1 = key;
    // String value1 = value.toString();
    });
    long endTime = System.currentTimeMillis();
    System.out.println("java8内置方法运行时间" + (endTime - startTime));
    }
    }
    100万条同时得到key和value所用的时间

    keySetGetKeyAndValue运行时间75----1000000
    keySetIteratorGetKeyAndValue运行时间82---------1000000
    entrySetGetKeyAndValue运行时间96-----1000000
    entrySetIteratorGetKeyAndValue运行时间66---1000000
    java8内置方法运行时间62

    只用到key时

    keySetGetKeyAndValue运行时间25----1000000
    keySetIteratorGetKeyAndValue运行时间22---------1000000
    entrySetGetKeyAndValue运行时间25-----1000000
    entrySetIteratorGetKeyAndValue运行时间24---1000000

    值用到value时

    keySetGetKeyAndValue运行时间79----1000000
    keySetIteratorGetKeyAndValue运行时间81---------1000000
    entrySetGetKeyAndValue运行时间92-----1000000
    entrySetIteratorGetKeyAndValue运行时间67---1000000
    java8内置方法运行时间57



  • 相关阅读:
    golang 中 sync包的 WaitGroup
    Go_20: Golang 中 time 包的使用
    mysql 同步数据到 ElasticSearch 的方案
    mysql 对应 binlog 查看
    python3.6爬虫总结-01
    Golang 之协程详解
    golang私服搭建
    Ubuntu vim设置
    密码校验规则
    golang密码校验
  • 原文地址:https://www.cnblogs.com/echo777/p/11799833.html
Copyright © 2011-2022 走看看