如何将地图流转换为单个地图

发布于 2025-01-13 17:08:19 字数 355 浏览 1 评论 0 原文

我需要使用流将 Stream>> 转换为 Map>

EntityType 是一个具有特定值的 enum,例如 ABC。流中有很多地图。

我想使用 Stream API 将它们全部连接到一个地图中。有什么办法吗?

I need to use streams to convert the Stream<Map<EntityType, Set<String>>> to Map<EntityType, Set<String>>.

The EntityType is an enum having certain values, let's say A, B and C. And there are lots of maps in the stream.

I want to concatenate all of them into one map using Stream API. Is there any way?

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

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

发布评论

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

评论(2

快乐很简单 2025-01-20 17:08:20

你可以尝试这样的事情:

public static void main(String[] args) {

    Map<EntityType, Set<String>> mapA = Map.of(EntityType.A, Set.of("1", "2", "3"), EntityType.B, Set.of("4", "5", "6"));
    Map<EntityType, Set<String>> mapB = Map.of(EntityType.A, Set.of("1", "4", "5"), EntityType.C, Set.of("4", "5", "6"));
    Map<EntityType, Set<String>> mapC = Map.of(EntityType.A, Set.of("3", "7", "5"), EntityType.B, Set.of("4", "9", "6"));
    
    // The stream calls you can try:
    Map<EntityType, Set<String>> result = Stream.of(mapA, mapB, mapC)
      .flatMap(map -> map.entrySet().stream())
      .collect(Collectors.toMap(keyValuePair -> keyValuePair.getKey(),
                                keyValuePair -> keyValuePair.getValue(),
                                (value1, value2) -> combine(value1, value2)));
}

private static <T> Set<T> combine(Set<T>... sets) {
    return Stream.of(sets)
      .flatMap(Set::stream)
      .collect(Collectors.toSet());
}

enum EntityType {
    A,
    B,
    C
}

You could try something like this:

public static void main(String[] args) {

    Map<EntityType, Set<String>> mapA = Map.of(EntityType.A, Set.of("1", "2", "3"), EntityType.B, Set.of("4", "5", "6"));
    Map<EntityType, Set<String>> mapB = Map.of(EntityType.A, Set.of("1", "4", "5"), EntityType.C, Set.of("4", "5", "6"));
    Map<EntityType, Set<String>> mapC = Map.of(EntityType.A, Set.of("3", "7", "5"), EntityType.B, Set.of("4", "9", "6"));
    
    // The stream calls you can try:
    Map<EntityType, Set<String>> result = Stream.of(mapA, mapB, mapC)
      .flatMap(map -> map.entrySet().stream())
      .collect(Collectors.toMap(keyValuePair -> keyValuePair.getKey(),
                                keyValuePair -> keyValuePair.getValue(),
                                (value1, value2) -> combine(value1, value2)));
}

private static <T> Set<T> combine(Set<T>... sets) {
    return Stream.of(sets)
      .flatMap(Set::stream)
      .collect(Collectors.toSet());
}

enum EntityType {
    A,
    B,
    C
}
混吃等死 2025-01-20 17:08:20

为此,您可以通过应用 flatMap()展平流中的地图。并用一个条目包裹每个条目。需要此步骤1.以避免源突变2.以避免发生UnsupportedOperationException如果不可修改,则引发。

然后应用collect()

由于 EntityType 是一个 enum,因此 supplier 提供的容器最合适的选择是 EnumMap,它是专为enum-keys设计,比HashMap具有更好的性能。

combine 方法中,merge() 应用于结果映射,以将流中的条目添加到其中。

public static <T extends Enum<T>, U> Map<T, Set<U>> collect(Stream<Map<T, Set<U>>> source,
                                                            Class<T> enumClass) {
    return source
            .flatMap(map -> map.entrySet().stream()
                    .map(entry -> Map.entry(entry.getKey(), new HashSet<>(entry.getValue()))))
            .collect(() -> new EnumMap<>(enumClass),
                    (map, entry) -> map.merge(entry.getKey(),
                                              entry.getValue(),
                                              (v1, v2) -> {v1.addAll(v2); return v1;}),
                    Map::putAll);
}

ma​​in()

public static void main(String[] args) {
    Stream<Map<EntityType, Set<String>>> source =
            Stream.of(Map.of(EntityType.A, Set.of("foo"), EntityType.B, Set.of("bar")),
                    Map.of(EntityType.A, Set.of("foo", "bar"), EntityType.B, Set.of("bar", "fiz")),
                    Map.of(EntityType.A, Set.of("foobar"), EntityType.B, Set.of("abc", "www")));

    System.out.println(collect(source, EntityType.class));
}

输出

{A=[bar, foobar, foo], B=[bar, abc, fiz, www]}

For that, you can flatten the maps in the stream by applying flatMap(). And wrap each entry with a new one. This step is required 1. to avoid mutation of the source and 2. to prevent an UnsupportedOperationException which will be raised if sets are unmodifiable.

And then apply collect().

Since EntityType is an enum the most appropriate choice for the container provided by the supplier will be an EnumMap which was designed specifically for enum-keys and has a better performance than HashMap.

Inside the combine method merge() is being applied on the resulting map to add entries from the stream into it.

public static <T extends Enum<T>, U> Map<T, Set<U>> collect(Stream<Map<T, Set<U>>> source,
                                                            Class<T> enumClass) {
    return source
            .flatMap(map -> map.entrySet().stream()
                    .map(entry -> Map.entry(entry.getKey(), new HashSet<>(entry.getValue()))))
            .collect(() -> new EnumMap<>(enumClass),
                    (map, entry) -> map.merge(entry.getKey(),
                                              entry.getValue(),
                                              (v1, v2) -> {v1.addAll(v2); return v1;}),
                    Map::putAll);
}

main()

public static void main(String[] args) {
    Stream<Map<EntityType, Set<String>>> source =
            Stream.of(Map.of(EntityType.A, Set.of("foo"), EntityType.B, Set.of("bar")),
                    Map.of(EntityType.A, Set.of("foo", "bar"), EntityType.B, Set.of("bar", "fiz")),
                    Map.of(EntityType.A, Set.of("foobar"), EntityType.B, Set.of("abc", "www")));

    System.out.println(collect(source, EntityType.class));
}

Output

{A=[bar, foobar, foo], B=[bar, abc, fiz, www]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文