HashMap的7种遍历方式

HashMap的遍历方式

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

  1. 迭代器(Iterator)方式遍历;
  2. For Each方式遍历;
  3. Lambda表达式遍历(jdk1.8之后);
  4. Streams API遍历(jdk1.8之后)。

在这4类中,又存在不同的遍历方式,因此可以细分为7类:

  1. 使用迭代器(Iterator)EntrySet的方式遍历;
  2. 使用迭代器(Iterator)KeySet的方式遍历;
  3. For Each EntrySet的方式遍历;
  4. For Each KeySet的方式遍历;
  5. 使用Lambda表达式的方式遍历;
  6. 使用Streams API 单线程的方式遍历;
  7. 使用Streams API 多线程的方式遍历。

具体代码例子

下面通过例子进行演示:

  1. 迭代器(Iterator)EntrySet方式
	public class One {
    //迭代器(Iterator)EntrySet方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

2. 迭代器(Iterator)KeySet方式

public class Two {
    //迭代器(Iterator)KeySet方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");
        
        //遍历
        Iterator<Integer> iterator = map.keySet().iterator();
        while(iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println(key);
            System.out.println(map.get(key));
        }
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

3. For Each EntrySet方式

public class Three {
    //ForEach EntrySet方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        for (Map.Entry<Integer, String> entry : map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

4. ForEach KeySet方式

public class Four {
    //ForEach KeySet方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");
        
        //遍历
        for (Integer key : map.keySet()){
            System.out.println(key);
            System.out.println(map.get(key));
        }
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

5. Lambda表达式方式

public class Five {
	//Lambda表达式方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value);
        });
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

6. Streams API 单线程方式

public class Six {
    //Streams API 单线程方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.entrySet().stream().forEach((entry) -> {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

7. Streams API 多线程方式

public class Seven {
    //Streams API 多线程方式
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.entrySet().parallelStream().forEach((entry) -> {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

执行结果:

1
Java
2
C++
3
C
4
Python
5
Golang

总结:综合性能和安全性来看,我们应该尽量使用迭代器(Iterator)EntrySet 方式来遍历 Map 集合。

参考:https://mp.weixin.qq.com/s/zQBN3UvJDhRTKP6SzcZFKw

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dzhuser

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值