HashMap七种常见遍历方式

HashMap 遍历

HashMap 遍历从大的方向来说,可分为以下 4 类

  • 迭代器(Iterator)方式遍历;

  • For Each 方式遍历;

  • Lambda 表达式遍历(JDK 1.8+);

  • Streams API 遍历(JDK 1.8+)。

但每种类型下又有不同的实现方式,因此具体的遍历方式又可以分为以下 7 种:

  • 使用迭代器(Iterator)EntrySet 的方式进行遍历;

  • 使用迭代器(Iterator)KeySet 的方式进行遍历;

  • 使用 For Each EntrySet 的方式进行遍历;

  • 使用 For Each KeySet 的方式进行遍历;

  • 使用 Lambda 表达式的方式进行遍历;

  • 使用 Streams API 单线程的方式进行遍历;

  • 使用 Streams API 多线程的方式进行遍历。

1.迭代器 EntrySet

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
//      第一种 迭代器 EntrySet
        Iterator<Map.Entry<String,Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Integer> entry = iterator.next();
            System.out.println(entry.getKey()+": "+entry.getValue());
           }
    }
}

运行结果:
k1: 1
k2: 2
k3: 3
k4: 4
k5: 5

2.迭代器 KeySet

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
//      第二种 迭代器 KeySet
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            System.out.println(key+": "+map.get(key));
           }
    }
}

3.ForEach EntrySet

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
        //for each
        for (Map.Entry<String,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+": "+entry.getValue());
        }
    }
}

4.ForEach KeySet

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
        //for each
        for (String key : map.keySet()) {
            System.out.println(key+": "+map.get(key));
        }
    }
}

5.Lambda

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
        //for each
        map.forEach((key,value) ->{
            System.out.println(key+": "+value);
        });
    }
}

6.Streams API 单线程

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
        map.entrySet().stream().forEach((entry) ->{
            System.out.println(entry.getKey()+": "+entry.getValue());
        });
    }
}

7.Streams API 多线程

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", 2);
        map.put("k3", 3);
        map.put("k4", 4);
        map.put("k5", 5);
        //for each
        map.entrySet().parallelStream().forEach((entry) ->{
            System.out.println(entry.getKey()+": "+entry.getValue());
        });
    }
}

执行结果:
k3: 3
k4: 4
k5: 5
k1: 1
k2: 2

总结:

这里我们使用了hashmap的四种遍历方式(迭代器、for、lambda、stream),以及具体的 7 种遍历方法,其中Lambda 表达式遍历和Streams API 遍历是在jdk1.8之后才有的,需要注意。希望这些能帮助到你

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值