Map遍历的 6 种方式!

在 Java 中,有多种遍历 Map 的方式。最常用的遍历方法包括使用 for-each 循环、Iterator 迭代器和 Stream API。以下是几种常见的遍历 Map 的方式:

1. 使用 for-each 循环遍历 Map.Entry

这是最常用的方式,使用 for-each 循环遍历 MapentrySet()

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

2. 使用 for-each 循环遍历 keySet

使用 for-each 循环遍历 MapkeySet(),然后通过 get 方法获取值。

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        for (String key : map.keySet()) {
            Integer value = map.get(key);
            System.out.println("Key: " + key + ", Value: " + value);
        }
    }
}

3. 使用 for-each 循环遍历 values

使用 for-each 循环遍历 Mapvalues(),如果只需要访问值而不需要访问键。

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        for (Integer value : map.values()) {
            System.out.println("Value: " + value);
        }
    }
}

4. 使用 Iterator 遍历 Map.Entry

使用 Iterator 遍历 MapentrySet(),适用于需要在遍历过程中进行删除操作的情况。

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

5. 使用 Stream API 遍历 Map

在 Java 8 及以上版本中,可以使用 Stream API 遍历 Map

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));

        // 或者使用 stream
        map.entrySet().stream()
           .forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
    }
}

6. 使用 Streams API 进行高级操作

Stream API 不仅可以用于遍历,还可以进行过滤、映射等操作。

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);

        // 过滤值大于1的条目
        Map<String, Integer> filteredMap = map.entrySet().stream()
            .filter(entry -> entry.getValue() > 1)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        filteredMap.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
    }
}

总结

以上是几种常用的遍历 Map 的方法,选择哪种方式取决于具体的需求和代码风格。在需要进行删除操作时,使用 Iterator 会更合适;在进行复杂的流式处理时,Stream API 是一个强大的工具。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值