1

给定一张这样的地图,我们有一年中每周每天的频率计数:

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

…或作为文本:

{周一=52,周二=52,周三=53,周四=53,周五=52,周六=52,周日=52}

...我如何反转以生成不同数字的多重映射,每个数字都导致DayOfWeek拥有该数字的集合(列表?集合?)?

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

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

我想使用直接 Java 生成生成的多图,而不需要额外的库,例如Eclipse CollectionsGoogle Guava。这些库可能会使这更容易,但我很想知道是否可以使用仅使用内置 Java 的解决方案。否则,我的问题与Guava 完全相同:通过反转 Map 来构造 Multimap。鉴于现代 Java 中的新流和多图特性,我希望现在可以实现,而当时还没有。

我看到了与此类似的各种现有问题。但没有一个适合我的情况,这似乎是一种相当普遍的情况。例如,这个问题忽略了原始值是冗余/多重的问题,因此需要一个多重映射。诸如此类或此类的其他涉及Google Guava。

4

4 回答 4

4

以下工作使用 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 的提交者。

于 2020-12-28T07:19:35.457 回答
1

使用流,您可以将映射拆分为其条目,然后翻转条目并分组:

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)))
  ));
于 2020-12-28T07:28:55.883 回答
1

Collectors.toMap

在这种情况下,您可以使用方法Collectors.toMap​(keyMapper,valueMapper,mergeFunction)并生成多图,其中值可以是列表集合

  1. 多图值为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. 多图值为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;
                    }));
    

另请参阅:收集基于多个字段的 id 列表

于 2021-01-02T15:45:37.267 回答
0

请参考以下代码:

@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);
}
于 2020-12-28T07:23:13.557 回答