【问题标题】:How to convert an MultiValueMap<String,String> to Map<String, List<Long>> with Stream? [closed]如何使用 Stream 将 MultiValueMap<String,String> 转换为 Map<String, List<Long>>? [关闭]
【发布时间】:2020-09-04 19:15:36
【问题描述】:

下面的代码 sn-p 接受多值映射并转换为映射。现在我希望地图使用流返回具有值 List 的 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())));

提前谢谢你!

【问题讨论】:

  • e.getValue() 是字符串,但你想存储在 Integer 的集合中,你如何定义 map 的 List&lt;Long&gt; ?也许你应该使用Map&lt;String, List&lt;String&gt;&gt;
  • 除此之外 - .filter(f -&gt; multi.containsKey("employee")) 与您共享的命令式方法中的任何行都没有关系,并且预期的类型也从 Map&lt;String, String&gt; 更改为 Map&lt;String, List&lt;Long&gt;&gt;
  • 在您的帖子中添加示例输入和输出,以明确您想要什么。
  • 您发布的代码中没有parseLong。您应该发布您尝试过的内容以及错误描述。
  • 问题中发布的代码尝试将结果分配给Map&lt;String, List&lt;Long&gt;&gt;,而没有尝试将String 转换为Long。当然,它没有通过编译器,需要修复。如果你保持原样,那就是答案。这将无济于事,因为您在评论中发布了您已经尝试插入 parseLong 调用并遇到错误,因此您应该帮自己一个忙并更改问题以准确显示您尝试了什么以及您遇到了哪个错误。

标签: java spring dictionary java-stream


【解决方案1】:

MultiValueMap&lt;String, String&gt; 转换为Map&lt;String, List&lt;Long&gt;&gt;,假设所有MultiValueMap 值都是数字字符串。

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

【讨论】:

  • 这对我有用。感谢您的投入 Eklavya! multi.entrySet().stream() .filter( e -> e.getKey().equalsIgnoreCase("employee")) .collect( Collectors.toMap( Map.Entry::getKey, e -> e.getValue() .stream().map(Long::parseLong).collect(Collectors.toList())));
【解决方案2】:

将方法转换为 Stream API 的解决方案相当简单,可以通过一个收集器完成:

Map<String, String> map = multi.entrySet().stream() // from Map<String, List<String>>
    .collect(Collectors.toMap(                      // Collected to Map<String, String>
            Entry::getKey,                          // ... the key remains the same
            e -> String.join(",", e.getValue())));  // ... the values are joined to String

String.join 方法能够将字符串集合转换为单个字符串,其中特定字符串与定义的分隔符连接。


此外,第一个 sn-p 中的Map&lt;String, String&gt; 与您在第二个Map&lt;String, List&lt;Long&gt;&gt; 中的尝试不兼容。

【讨论】:

  • 但我正在寻找将 MultiValueMap 转换为 Map> 我该怎么做?
  • 提供样本输入输出。你想如何从 String 中获取 List?你说的不清楚。
【解决方案3】:
 Map<String, List<Long>> map = multi.entrySet().stream()
      .filter(f -> multi.containsKey("employee"))
      .flatMap(e->e.getValue().stream()
      .map(v->new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v)))
      .collect(Collectors.toMap(Map.Entry ::getKey,Collectors.mapping(e -> e.getValue(), Collectors.toList())));

【讨论】:

  • 我仍然收到“无法解析 'Object' 中的方法 'getValue'”错误。
猜你喜欢
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 2021-12-21
  • 1970-01-01
  • 2015-11-21
相关资源
最近更新 更多