• map遍历方式以及消耗时间


    啥也不别说,直接上代码:忽略说的这句话!

    package com.linyang.ForTest;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    public class ForMap {
    
    	// 循环遍历map的方法
    	public static void main(String[] args) {
    		System.out.println("ready");
    		long start = System.currentTimeMillis();
    		Map<String, String> dataMap = new HashMap<String, String>();
    
    		String keyString = "key-";
    		int in = 0;
    
    		for (int i = 0; i < 10000000; i++) {
    			dataMap.put(keyString + i, String.valueOf(i));
    			in = i + 1;
    		}
    		long end = System.currentTimeMillis();
    		long UseTime = end - start;
    		System.out.println("ready time :" + UseTime);
    
    		System.out.println("start");
    		start = System.currentTimeMillis();
    		// 方法一 hashmap entrySet()
    		Iterator it = dataMap.entrySet().iterator();
    		while (it.hasNext()) {
    			Map.Entry entry = (Map.Entry) it.next();
    			Object key = entry.getKey();
    			Object value = entry.getValue();
    //			System.out.println("key=" + key + " value=" + value);
    		}
    		end = System.currentTimeMillis();
    		UseTime = end - start;
    		System.out.println("########## entrySet-UseTime ########## :" + UseTime);
    		// For-Each循环
    		// 方法二
    		start = System.currentTimeMillis();
    		for (Entry<String, String> entry : dataMap.entrySet()) {
    			String key = entry.getKey().toString();
    			String value = entry.getValue().toString();
    //			System.out.println("key=" + key + " value=" + value);
    		}
    		end = System.currentTimeMillis();
    		UseTime = end - start;
    		System.out.println("##########For-Each UseTime########## :" + UseTime);
    
    		// hashmap keySet() 遍历
    		start = System.currentTimeMillis();
    		for (Iterator i = dataMap.keySet().iterator(); i.hasNext();) {
    			Object obj = i.next();
    //			System.out.println("key=" + obj + " value=" + dataMap.get(obj));
    		}
    		end = System.currentTimeMillis();
    		UseTime = end - start;
    		System.out.println("##########hashmap keySet() UseTime########## :" + UseTime);
    		start = System.currentTimeMillis();
    		for (Iterator i = dataMap.values().iterator(); i.hasNext();) {
    			String obj = (String) i.next();
    //			System.out.println(obj);// 循环输出value
    
    		}
    		end = System.currentTimeMillis();
    		UseTime = end - start;
    		System.out.println("##########hashmap keySet() UseTime######### :" + UseTime);
    
    		start = System.currentTimeMillis();
    		// 方法四 treemap keySet()遍历
    		for (Object o : dataMap.keySet()) {
    //			System.out.println("key=" + o + " value=" + dataMap.get(o));
    		}
    		end = System.currentTimeMillis();
    		UseTime = end - start;
    		System.out.println("###########treemap keySet() UseTime######### :" + UseTime);
    
    		// java如何遍历Map <String, ArrayList> map = new HashMap <String,
    		// ArrayList>();
    		start = System.currentTimeMillis();
    		System.out.println("java  遍历Map <String, ArrayList> map = new HashMap<String, ArrayList>();");
    		Map<String, ArrayList> map = new HashMap<String, ArrayList>();
    		Set<String> keys = map.keySet();
    		Iterator<String> iterator = keys.iterator();
    		while (iterator.hasNext()) {
    			String key = iterator.next();
    			ArrayList arrayList = map.get(key);
    			for (Object o : arrayList) {
    				System.out.println(o);
    			}
    		}
    
    		start = System.currentTimeMillis();
    		Map<String, List> map1 = new HashMap<String, List>();
    		for (Map.Entry entry : map1.entrySet()) {
    			String key = entry.getKey().toString();
    			List<String> list = (List) entry.getValue();
    			for (String value : list) {
    				System.out.println(key + "====" + value);
    			}
    		}
    
    	}
    
    }
    

      

    直接说结论:entrySet() 速度最快

    ready
    ready time :15284
    start
    ########## entrySet-UseTime ########## :186
    ##########For-Each UseTime########## :236
    ##########hashmap keySet() UseTime########## :205
    ##########hashmap keySet() UseTime######### :216
    ###########treemap keySet() UseTime######### :201

  • 相关阅读:
    spring @resource @ Autowired
    mysql 。。。
    MYSQL
    oracle sql 性能 优化
    tomcat 解析(五)-Tomcat的核心组成和启动过程
    tomcat 解析(四)-处理http请求过程
    tomcat 解析(三)-启动框架
    tomcat 解析(二)-消息处理过程
    tomcat 解析(一)-文件解析
    我发起并创立了一个 C 语言编译器 开源项目 InnerC
  • 原文地址:https://www.cnblogs.com/adao21/p/12560685.html
Copyright © 2020-2023  润新知