反转具有冗余值的 Map 以生成多图

Invert a Map with redundant values to produce a multimap

给定这样的地图,其中我们有一年中每个星期几的频率计数:

Map.of(
    DayOfWeek.MONDAY , 52 ,
    DayOfWeek.TUESDAY , 52 ,
    DayOfWeek.WEDNESDAY, 53 ,
    DayOfWeek.THURSDAY , 53 ,
    DayOfWeek.FRIDAY , 52 ,
    DayOfWeek.SATURDAY , 52 ,
    DayOfWeek.SUNDAY , 52 
)

...或作为文本:

{MONDAY=52, TUESDAY=52, WEDNESDAY=53, THURSDAY=53, FRIDAY=52, SATURDAY=52, SUNDAY=52}

…我如何反转以生成一个 multimap 个不同的数字,每个数字都指向一个集合(列表?集合?)DayOfWeek 拥有该数字的集合?

结果应该等同于这段代码的结果:

Map.of(
    53 , List.of( DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY ) ,
    52 , List.of( DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.FRIDAY , DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) 
)

我想直接使用 Java 生成结果 multimap 而无需额外的库,例如 Eclipse CollectionsGoogle Guava。这些库可能会使这更容易,但我很好奇是否可以使用仅内置 Java 的解决方案。否则,我这里的问题与 Guava 完全相同:通过反转 Map 构造一个 Multimap。考虑到现代 Java 中的新流和多图功能,我希望这在现在是可能的,而在当时是不可能的。

我看到了各种与此类似的现有问题。但是 none 符合我的情况,这似乎是一种相当普遍的情况。例如,this Question neglects the issue of the original values being redundant/multiple, thus necessitating a multimap as a result. Others such as this or this涉及GoogleGuava。

以下使用 Java 9 或更高版本的作品:

@Test
void invertMap()
{
    Map<DayOfWeek, Integer> map = Map.of(
            DayOfWeek.MONDAY, 52,
            DayOfWeek.TUESDAY, 52,
            DayOfWeek.WEDNESDAY, 53,
            DayOfWeek.THURSDAY, 53,
            DayOfWeek.FRIDAY, 52,
            DayOfWeek.SATURDAY, 52,
            DayOfWeek.SUNDAY, 52
    );

    Map<Integer, Set<DayOfWeek>> flipped = new TreeMap<>();
    map.forEach((dow, count) ->
            flipped.computeIfAbsent(count, (key) ->
                    EnumSet.noneOf(DayOfWeek.class)).add(dow));

    Map<Integer, Set<DayOfWeek>> flippedStream = map.entrySet().stream()
           .collect(Collectors.groupingBy(
                    Map.Entry::getValue, 
                    TreeMap::new,
                    Collectors.mapping(
                            Map.Entry::getKey,
                            Collectors.toCollection(
                                    () -> EnumSet.noneOf(DayOfWeek.class)))));

    Map<Integer, Set<DayOfWeek>> expected = Map.of(
            53, EnumSet.of(
                    DayOfWeek.WEDNESDAY, 
                    DayOfWeek.THURSDAY),
            52, EnumSet.of(
                    DayOfWeek.MONDAY, 
                    DayOfWeek.TUESDAY, 
                    DayOfWeek.FRIDAY, 
                    DayOfWeek.SATURDAY, 
                    DayOfWeek.SUNDAY)
    );
    Assert.assertEquals(expected, flipped);
    Assert.assertEquals(expected, flippedStream);
}

如果您愿意使用第三方库,以下代码将适用于 Eclipse Collections

@Test
void invertEclipseCollectionsMap()
{
    MutableMap<DayOfWeek, Integer> map =
            Maps.mutable.<DayOfWeek, Integer>empty()
                    .withKeyValue(DayOfWeek.MONDAY, 52)
                    .withKeyValue(DayOfWeek.TUESDAY, 52)
                    .withKeyValue(DayOfWeek.WEDNESDAY, 53)
                    .withKeyValue(DayOfWeek.THURSDAY, 53)
                    .withKeyValue(DayOfWeek.FRIDAY, 52)
                    .withKeyValue(DayOfWeek.SATURDAY, 52)
                    .withKeyValue(DayOfWeek.SUNDAY, 52);

    SetMultimap<Integer, DayOfWeek> flipped = map.flip();

    Assert.assertEquals(flipped.get(52), Set.of(
            DayOfWeek.MONDAY,
            DayOfWeek.TUESDAY,
            DayOfWeek.FRIDAY,
            DayOfWeek.SATURDAY,
            DayOfWeek.SUNDAY));
    Assert.assertEquals(flipped.get(53), Set.of(
            DayOfWeek.WEDNESDAY,
            DayOfWeek.THURSDAY));
}

注意:我是 Eclipse Collections 的提交者。

请参考以下代码:

@Test
void testMap() {
    Map<DayOfWeek, Integer> map = new HashMap<>();
    map.put(DayOfWeek.MONDAY, 52);
    map.put(DayOfWeek.TUESDAY, 52);
    map.put(DayOfWeek.WEDNESDAY, 53);
    map.put(DayOfWeek.THURSDAY, 53);
    map.put(DayOfWeek.FRIDAY, 52);
    map.put(DayOfWeek.SATURDAY, 52);
    map.put(DayOfWeek.SUNDAY, 52);

    Map<Integer, List<DayOfWeek>> result = new HashMap<>();

    for (Map.Entry<DayOfWeek, Integer> entry : map.entrySet()) {
        if (result.containsKey(entry.getValue())) {
            List list = result.get(entry.getValue());
            list.add(entry.getKey());
            result.put(entry.getValue(), list);
        } else {
            List list = new ArrayList();
            list.add(entry.getKey());
            result.put(entry.getValue(), list);
        }
    }
    System.out.println(result);
}

使用流,您可以将地图拆分为其条目,然后翻转条目和组:

numberOfDaysInYear.entrySet().stream()
  .collect(groupingBy(Map.Entry::getValue), mapping(Map.Entry::getKey, toList()));

根据您更新后的评论,要求优化实际上并不在您的原始问题中,

numberOfDaysInYear.entrySet().stream()
  .collect(groupingBy(
    Map.Entry::getValue,
    TreeMap::new,
    mapping(Map.Entry::getKey, toCollection(() -> EnumSet.of(DayOfWeek.class)))
  ));

Collectors.toMap

在这种情况下,您可以使用方法 Collectors.toMap​(keyMapper,valueMapper,mergeFunction) 并生成 multimap,其中值可以是 listset :

  1. Multimap 值是 List:
    Map<Integer, List<DayOfWeek>> inverted = map.entrySet().stream()
            .collect(Collectors.toMap(
                    // key of the new map
                    entry -> entry.getValue(),
                    // value of the new map
                    entry -> List.of(entry.getKey()),
                    // merging two values, i.e. lists
                    (list1, list2) -> {
                        List<DayOfWeek> list = new ArrayList<>();
                        list.addAll(list1);
                        list.addAll(list2);
                        return list;
                    }));
    
  2. Multimap 值是 Set
    Map<Integer, Set<DayOfWeek>> inverted = map.entrySet().stream()
            .collect(Collectors.toMap(
                    // key of the new map
                    entry -> entry.getValue(),
                    // value of the new map
                    entry -> Set.of(entry.getKey()),
                    // merging two values, i.e. sets
                    (set1, set2) -> {
                        Set<DayOfWeek> set = new HashSet<>();
                        set.addAll(set1);
                        set.addAll(set2);
                        return set;
                    }));
    

另请参阅: