可以通过Java8的Stream API和Collectors工具类实现:

Map<String, Set<String>> inputMap = new HashMap<>();
// 假设已经将inputMap填充了数据

Map<String, List<String>> outputMap = inputMap.entrySet()
        .stream()
        .collect(Collectors.toMap(
                Map.Entry::getKey,
                entry -> new ArrayList<>(entry.getValue())
        ));

首先将Map<String, Set<String>>转换为Stream<Map.Entry<String, Set<String>>>,然后使用Collectors.toMap()将其转换为Map<String, List<String>>。在toMap()中,我们将输入Map中的每个键值对转换为输出Map中的键值对,将键保留不变,将值从Set<String>转换为List<String>。注意,为了确保输出Map中的值是可变的,我们使用new ArrayList<>(entry.getValue())创建了一个新的ArrayList对象来存储Set中的元素。

Java8 将 Map<String, Set<String>> 转换为 Map<String, List<String>>

原文地址: https://www.cveoy.top/t/topic/nx1K 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录