取出集合中的key和value

通过keySet的for循环方式获取Map中的key

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值