Java-如何取出集合Map中key和value的值

修订记录 版本 是否发布
2016-01-25 v1.0

一、通过keySet的for循环方式获取Map中的key,value

	public static void keySetForGetKeyValue(Map<String, String> map) {
		long startTime = System.currentTimeMillis();
		for (String key : map.keySet()) {
			String v = map.get(key);
		}
		long endTime = System.currentTimeMillis();
		System.out.println("keySet的for循环方式运行时间:" + (endTime - startTime));
	}

100W数据执行平均耗时:173.1毫秒

keySet的for循环方式运行时间:272
keySet的for循环方式运行时间:141
keySet的for循环方式运行时间:246
keySet的for循环方式运行时间:187
keySet的for循环方式运行时间:153
keySet的for循环方式运行时间:210
keySet的for循环方式运行时间:162
keySet的for循环方式运行时间:103
keySet的for循环方式运行时间:112
keySet的for循环方式运行时间:145
平均运行时间:173.1

二、通过keySet的iterator迭代器方式获取Map中的key,value

	public static void keySetIteratorGetKeyValue(Map<String, String> map) {
		long startTime = System.currentTimeMillis();
		Iterator<String> iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			String value = map.get(key);
		}
		long endTime = System.currentTimeMillis();
		System.out.println("keySet的iterator迭代器方式运行时间:" + (endTime - startTime));

100W数据执行平均耗时:146.1毫秒

keySet的iterator迭代器方式运行时间:280
keySet的iterator迭代器方式运行时间:133
keySet的iterator迭代器方式运行时间:188
keySet的iterator迭代器方式运行时间:157
keySet的iterator迭代器方式运行时间:149
keySet的iterator迭代器方式运行时间:79
keySet的iterator迭代器方式运行时间:88
keySet的iterator迭代器方式运行时间:114
keySet的iterator迭代器方式运行时间:142
keySet的iterator迭代器方式运行时间:131
平均运行时间:146.1

三、通过entrySet的for循环方式获取Map中的key,value(推荐)

public static void entrySetForGetKeyValue(Map<String, String> map) {
		long startTime = System.currentTimeMillis();
		for (Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("entrySet的for循环方式运行时间:" + (endTime - startTime));
	}

100W数据执行平均耗时:87.6毫秒

entrySet的for循环方式运行时间:153
entrySet的for循环方式运行时间:85
entrySet的for循环方式运行时间:113
entrySet的for循环方式运行时间:104
entrySet的for循环方式运行时间:123
entrySet的for循环方式运行时间:61
entrySet的for循环方式运行时间:88
entrySet的for循环方式运行时间:51
entrySet的for循环方式运行时间:57
entrySet的for循环方式运行时间:41
平均运行时间:87.6

四、通过entrySet的iterator迭代器方式获取Map中的key,value

public static void entrySetIteratorGetKeyValue(Map<String, String> map) {
		long startTime = System.currentTimeMillis();
		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next().getKey();
			String value = iterator.next().getValue();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("通过entrySet的iterator迭代器方式运行时间:" + (endTime - startTime));
	}

100W数据执行平均耗时:95.4毫秒

通过entrySet的iterator迭代器方式运行时间:134
通过entrySet的iterator迭代器方式运行时间:76
通过entrySet的iterator迭代器方式运行时间:113
通过entrySet的iterator迭代器方式运行时间:156
通过entrySet的iterator迭代器方式运行时间:62
通过entrySet的iterator迭代器方式运行时间:70
通过entrySet的iterator迭代器方式运行时间:138
通过entrySet的iterator迭代器方式运行时间:71
通过entrySet的iterator迭代器方式运行时间:57
通过entrySet的iterator迭代器方式运行时间:77
平均运行时间:95.4

五、通过JDK1.8中map.forEach方式获取Map中的key,value(强烈推荐)

	public static void MapForEachGetKeyValue(Map<String, String> map) {
		long startTime = System.currentTimeMillis();
		map.forEach((k,v)->{

		});
		long endTime = System.currentTimeMillis();
		System.out.println("JDK1.8中map.forEach方式运行时间:" + (endTime - startTime));
	}

100W数据执行平均耗时:86.6毫秒

JDK1.8中map.forEach方式运行时间:346
JDK1.8中map.forEach方式运行时间:50
JDK1.8中map.forEach方式运行时间:80
JDK1.8中map.forEach方式运行时间:49
JDK1.8中map.forEach方式运行时间:50
JDK1.8中map.forEach方式运行时间:56
JDK1.8中map.forEach方式运行时间:50
JDK1.8中map.forEach方式运行时间:69
JDK1.8中map.forEach方式运行时间:81
JDK1.8中map.forEach方式运行时间:35
平均运行时间:86.6
posted @ 2022-08-23 11:28  编程秀  阅读(2542)  评论(0编辑  收藏  举报