map遍历方式以及消耗时间

Posted adao21

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于map遍历方式以及消耗时间的主要内容,如果未能解决你的问题,请参考以下文章

Map对象中的keyset()entryset()和Map.Entry 以及 四种遍历map的方式

Java中Map的 entrySet() 详解以及用法(四种遍历map的方式)

201621123037 《Java程序设计》第9周学习总结

遍历Map的几种方式

map的两种遍历方式是什么

JS几种数组遍历方式总结