HashMap循环遍历

HashMap的七种循环遍历方法

public class mapDemo {

    public static void main(String[] args) {
        // 创建并赋值HashMap
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Java");
        map.put(2, "C语言");
        map.put(3, "php");

        // 遍历map
        // 第一种方法  迭代器EntrySet
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            System.out.print(next.getKey());
            System.out.print(next.getValue());
        }
        System.out.println("---------------");
        // 第二种方法 迭代器 keySet
        Iterator<Integer> iterator1 = map.keySet().iterator();
        while (iterator1.hasNext()){
            Integer next = iterator1.next();
            System.out.print(next);
            System.out.print(map.get(next));
        }
        System.out.println("---------------");
        // 第三种 forEach EntrySet
        for (Map.Entry<Integer, String> next: map.entrySet()) {
            System.out.print(next.getKey());
            System.out.print(next.getValue());
        }
        System.out.println("---------------");
        // 第四种 forEach Keyset
        for(Integer key : map.keySet()){
            System.out.print(key);
            System.out.print(map.get(key));
        }
        System.out.println("---------------");
        // 第五种 lambda
        map.forEach((key, value) ->{
            System.out.print(key);
            System.out.print(value);
        });
        System.out.println("---------------");
        // 第六种 Streams Api 单线程
        map.entrySet().stream().forEach((entry) ->{
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        });
        System.out.println("---------------");
        // 第七种 Streams API 多线程
        map.entrySet().parallelStream().forEach((entry) ->{
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        });
        System.out.println("---------------");
    }
}

运行结果:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
HashMap遍历原理如下: 1. 首先,HashMap会将所有的键值对存储在一个数组中,这个数组被称为哈希桶(hash bucket)。 2. 当我们向HashMap中插入一个键值对时,HashMap会根据键的哈希码计算出一个索引值,然后将键值对存储在对应的索引位置上。 3. 当我们要遍历HashMap时,可以通过遍历哈希桶来获取所有的键值对。 4. 遍历哈希桶的方式有两种:迭代器(Iterator)和增强for循环(foreach)。 - 使用迭代器遍历HashMap时,首先通过调用HashMap的entrySet()方法获取到一个包含所有键值对的Set集合,然后通过调用Set集合的iterator()方法获取到一个迭代器,最后使用迭代器的next()方法和hasNext()方法来遍历键值对。 - 使用增强for循环遍历HashMap时,直接使用HashMap的entrySet()方法获取到一个包含所有键值对的Set集合,然后使用增强for循环遍历键值对。 下面是使用迭代器和增强for循环遍历HashMap的示例代码: ```java import java.util.HashMap;import java.util.Iterator; import java.util.Map; public class HashMapTraversalExample { public static void main(String[] args) { // 创建一个HashMap HashMap<String, Integer> hashMap = new HashMap<>(); hashMap.put("apple", 1); hashMap.put("banana", 2); hashMap.put("orange", 3); // 使用迭代器遍历HashMap Iterator<Map.Entry<String, Integer>> iterator = hashMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Integer> entry = iterator.next(); String key = entry.getKey(); Integer value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); } // 使用增强for循环遍历HashMap for (Map.Entry<String, Integer> entry : hashMap.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

等风,也等你

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

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

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

打赏作者

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

抵扣说明:

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

余额充值