HashMap的7种遍历方式

HashMap的遍历方式有七种?经常用的感觉就两种,一种用迭代器,一种ForEach(增强型for循环),感觉就没了。网上查了一下,还真有其他的。今天总结一下。


 四大类遍历方式

在JDK1.8之前,遍历方式还没有这样多地,为了提高开发效率,JDK1.8以后引入了Stream流,Lambda表达式等新特性。七种遍历方式看起来很多,其实可以分成四个大类:

迭代器方式遍历

foreach方式遍历

Lambda表达式遍历

Stream流方式遍历

它们又根据具体的实现形式分成多个小类:

1.迭代器方式遍历

EntrySet遍历方式

public class Test01 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","2");
        hashMap.put("2","2");
        hashMap.put("3","2");
        hashMap.put("4","2");
        hashMap.put("5","2");
        hashMap.put("6","2");
        //迭代器
        Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            System.out.println(next.getKey()+" "+next.getValue());
        }
    }
}

KeySet遍历方式

package com.pf.gather;

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

public class Test02 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
        Iterator<String> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            System.out.println(hashMap.get(key));
        }
    }
}

foreach方式遍历

EntrySet遍历方式

package com.pf.gather;

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

public class Test03 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
        for(Map.Entry<String,String> entry:hashMap.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }
}

KeySet遍历方式

package com.pf.gather;

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

public class Test04 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
        for (String str:hashMap.keySet()){
            System.out.println(hashMap.get(str));
        }
    }
}

Lambda表达式遍历

package com.pf.gather;

import java.util.HashMap;

public class Test05 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
       hashMap.forEach((key,value)->{
           System.out.println(key+" "+value);
       });
    }
}

Stream流方式遍历

Streams API 单线程场景下遍历方式

package com.pf.gather;

import java.util.HashMap;

public class Test06 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
        hashMap.entrySet().stream().forEach((entry)->{
            System.out.println(entry.getKey()+" "+entry.getValue());
        });
    }
}

Streams API 多线程场景下遍历方式

package com.pf.gather;

import java.util.HashMap;

public class Test07 {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","21");
        hashMap.put("2","22");
        hashMap.put("3","23");
        hashMap.put("4","24");
        hashMap.put("5","25");
        hashMap.put("6","26");
        hashMap.entrySet().parallelStream().forEach((entry)->{
            System.out.println(entry.getKey()+" "+entry.getValue());
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值