交换和弄平Java地图< integer,list< integer>>>使用流API

发布于 2025-02-13 13:15:17 字数 762 浏览 0 评论 0原文

拥有此地图< integer,list>:

Map<Integer, List<Integer>> forwardMap = Map.of(
        100, List.of(6),
        300, List.of(49, 52),
        500, List.of(293)
);

我想“弄平”值列表,并在地图中交换键和值,最终以此结束:

Map<Integer, Integer> reverseMap = Map.of(
         6, 100,
        49, 300
        52, 300,
       293, 500
);

我的无法编译的尝试,我尝试流式传输set&lt;映射。进入&gt;然后是嵌套列表:

Map<Integer, Integer> reverseMap = forwardMap.entrySet().stream().map(
        entry -> entry.getValue().stream().collect(Collectors.toMap(Integer::getInteger, entry.getKey()));
);

也许我需要避免两次使用stream() - 可能是在某个地方使用flatmap()。我还尝试过首先交换键和值 - 但最终仍未引用列表中列表中“外部”键和“内部”嵌套整数的引用。

我想念什么或彻底误解?

Having this Map<Integer, List>:

Map<Integer, List<Integer>> forwardMap = Map.of(
        100, List.of(6),
        300, List.of(49, 52),
        500, List.of(293)
);

I would like to 'flatten' the value Lists and swap the key and value in the Map, ending up with this:

Map<Integer, Integer> reverseMap = Map.of(
         6, 100,
        49, 300
        52, 300,
       293, 500
);

My cannot-compile attempt, where I attempt to stream the Set<Map.Entry> and then the nested List:

Map<Integer, Integer> reverseMap = forwardMap.entrySet().stream().map(
        entry -> entry.getValue().stream().collect(Collectors.toMap(Integer::getInteger, entry.getKey()));
);

Perhaps I need to avoid using stream() twice - possibly by using flatMap() somewhere and somehow. I have also tried first swapping swapping the key and value - but still end up not having a reference to the 'outer' key and the 'inner' nested Integers in the Lists, at the same time.

What am I missing or downright misunderstanding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2025-02-20 13:15:17

这是一个类似的答案,并添加了一些内容。

  • 您的结果中的键进行了排序,因此我按顺序排序了。
  • 就您而言,最终结果中没有重复的键。如果发生这种情况,则过程将引发异常。有三个选择。
    • 保持第一个重复及其相关的值遇到的值,从而导致数据丢失。
    • 保持具有相同效果的最后一个重复。
    • 重复键的列表中的返回值。
  • 在此练习中,我通过(首先,下一个) - &gt; first合并函数选择了第一个选项。
  • 我还将项目返回linkedhashmap以保留排序订单。
Map<Integer, Integer> result = forwardMap.entrySet().stream()
        .flatMap(e -> e.getValue().stream()
                .map(v -> Map.entry(v, e.getKey())))
        .sorted(Entry.comparingByKey())
        .collect(Collectors.toMap(Entry::getKey,
                Entry::getValue, (first, next) -> first,
                LinkedHashMap::new));

result.entrySet().forEach(System.out::println);

如果您具有重复的价值并希望保留它,则在这里打印

6=100
49=300
52=300
293=500

是如何工作的。唯一的区别是最终收藏家。

  • groupingby用于为每个键创建
  • 一个linkedhashmap的列表,以保留排序的顺序。
  • 并使用映射收集器从条目中提取所需的值。
Map<Integer, List<Integer>> forwardMap =
        Map.of(100, List.of(6), 300, List.of(49, 52), 500,
                List.of(293, 52));

Map<Integer, List<Integer>> result2 =
        forwardMap.entrySet().stream()
                .flatMap(e -> e.getValue().stream()
                        .map(v -> Map.entry(v, e.getKey())))
                .sorted(Entry.comparingByKey())
                .collect(Collectors.groupingBy(
                        Map.Entry::getKey, LinkedHashMap::new,
                        Collectors.mapping(
                                Map.Entry::getValue,
                                Collectors.toList())));

result2.entrySet().forEach(System.out::println);

印刷

6=[100]
49=[300]
52=[500, 300]
293=[500]

Here is a similar answer with a few things added.

  • the keys in your result are sorted so I sorted them in ascending order.
  • In your case, there are no duplicate keys in the final result. If that happens the process will throw an exception. There are three options.
    • keep the first duplicate and its associated value encountered which results in loss of data.
    • keep the last duplicate which has the same effect.
    • return values in a list for duplicate keys.
  • in this exercise, I chose the first option via (first, next)->first merge function.
  • I also return the items in a LinkedHashMap to preserve the sorted order.
Map<Integer, Integer> result = forwardMap.entrySet().stream()
        .flatMap(e -> e.getValue().stream()
                .map(v -> Map.entry(v, e.getKey())))
        .sorted(Entry.comparingByKey())
        .collect(Collectors.toMap(Entry::getKey,
                Entry::getValue, (first, next) -> first,
                LinkedHashMap::new));

result.entrySet().forEach(System.out::println);

prints

6=100
49=300
52=300
293=500

Here is how it would work if you had a duplicate value and wanted to keep it. The only differences are in the final collector.

  • groupingBy is used to create a list for each key
  • a LinkedHashMap is specified to preserve the sorted order.
  • and a mapping collector is used to extract the desired value from the entry.
Map<Integer, List<Integer>> forwardMap =
        Map.of(100, List.of(6), 300, List.of(49, 52), 500,
                List.of(293, 52));

Map<Integer, List<Integer>> result2 =
        forwardMap.entrySet().stream()
                .flatMap(e -> e.getValue().stream()
                        .map(v -> Map.entry(v, e.getKey())))
                .sorted(Entry.comparingByKey())
                .collect(Collectors.groupingBy(
                        Map.Entry::getKey, LinkedHashMap::new,
                        Collectors.mapping(
                                Map.Entry::getValue,
                                Collectors.toList())));

result2.entrySet().forEach(System.out::println);

prints

6=[100]
49=[300]
52=[500, 300]
293=[500]
这样的小城市 2025-02-20 13:15:17

作为目标的一部分,是使值平板,您正确您可能需要在某个地方进行Flatmap操作。例如:

Map<Integer, Integer> reverseMap =
    forwardMap.entrySet().stream()
        .flatMap(
            entry -> entry.getValue().stream().map(value -> Map.entry(value, entry.getKey())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

As part of your goal is to flatten the values, you're correct you'll probably need a flatMap operation somewhere. For example:

Map<Integer, Integer> reverseMap =
    forwardMap.entrySet().stream()
        .flatMap(
            entry -> entry.getValue().stream().map(value -> Map.entry(value, entry.getKey())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文