gpt4 book ai didi

java - 如何使用 Stream 将 MultiValueMap 转换为 Map>?

转载 作者:行者123 更新时间:2023-12-01 14:09:10 28 4
gpt4 key购买 nike

<分区>

下面的代码片段接受多值映射并转换为映射。现在我希望 map 使用流返回具有值列表的 map 。

  public static Map<String, String> decodeMap(MultiValueMap<String, String> multi) {
if (CollectionUtils.isEmpty(multi)) {
return Map.of();
}
Map<String, String> map = new HashMap<>(multi.entrySet().size());
for (Map.Entry<String, List<String>> entry : multi.entrySet()) {
StringBuilder sb = new StringBuilder();
for (String s : entry.getValue()) {
if (sb.length() > 0) {
sb.append(',');
}
sb.append(s);
}
map.put(entry.getKey(), sb.toString());
}
return map;
}

这是我尝试过的方法,但效果不佳:

  Map<String, List<Long>> map = multi.entrySet().stream()
.filter(f -> multi.containsKey("employee"))
.collect(Collectors.toMap(Entry ::getKey,Collectors.mapping(e -> Long.parseLong(e.getValue()), Collectors.toList())));

提前致谢!

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com