按嵌套映射的值对外部映射进行排序 [英] Sort the outer map by the value of the nested map

查看:94
本文介绍了按嵌套映射的值对外部映射进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表大小排序外部地图地图<字符串,地图<字符串,列表<整数>>> 嵌套映射中的保留外键和内键,如前所述。

Sorting the outer map Map<String, Map<String, List<Integer>>> by the list size in the nested map retaining the outer and inner keys as before.

推荐答案

您可以通过概括流程来解决这个问题:

You may solve this by generalizing the process:

private static <K,V,R> Map<K,R>
                       replaceAndSortValues(Map<K,V> m, Function<V,R> f, Comparator<R> c) {
    return m.entrySet().stream()
        .map(e -> Map.entry(e.getKey(), f.apply(e.getValue())))
        .sorted(Map.Entry.comparingByValue(c.reversed()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                                  (a,b) -> { throw new AssertionError(); },
                                  LinkedHashMap::new));
}

此方法使用与指定键相同的键创建新映射,替换使用指定函数的所有值,并根据指定比较器的反转对条目进行排序。它使用Java 9的 Map.entry(...,...)工厂。如果必须支持Java 8或 null 键或值,则可以使用 new AbstractMap.SimpleImmutableEntry<>(...,...)而不是。

This method creates a new map with the same keys as the specified one, replacing all values using the specified function and sorting the entries according to the reversal of the specified comparator. It uses Java 9’s Map.entry(…, …) factory. If you have to support Java 8 or null keys or values, you may use new AbstractMap.SimpleImmutableEntry<>(…, …) instead.

此方法现在可用于用<$替换内部地图的列表 c $ c> Integer 表示它们的大小并按降序排序,并使用替换操作作为外部地图的替换函数:

This method can now be used to replace you inner map’s Lists with Integers representing their sizes and sort them in descending order and use the replacement operation as replacement function of the outer map:

public static Map<String, Map<String, Integer>>
              getCallWithStateSizeGroup(ThreadDumpDo threadDumpDo) {
    return replaceAndSortValues(getCallStackWithStateGroup(threadDumpDo),
        m -> replaceAndSortValues(m, List::size, Comparator.<Integer>naturalOrder()),
        Comparator.comparing(m -> m.values().iterator().next()));
}

这与您发布的解决方案基本相同。外部地图的比较器使用新内部地图已经排序的事实,因此它们的第一个值是最大值。但是必须没有空的内部地图。

This does basically the same as your posted solution. The outer map’s comparator uses the fact that the new inner maps are already sorted, so their first value is the maximum. But there must be no empty inner map.

这很容易适应以保持列表< ThreadDo> 和只需按大小排序:

This could easily get adapted to keep the List<ThreadDo> and just sort them by size:

public static Map<String, Map<String, List<ThreadDo>>>
              getCallWithStateSizeGroup(ThreadDumpDo threadDumpDo) {
    return replaceAndSortValues(getCallStackWithStateGroup(threadDumpDo),
        m -> replaceAndSortValues(m, Function.identity(),
                                  Comparator.comparingInt(List::size)),
        Comparator.comparingInt(m -> m.values().iterator().next().size()));
}

我们只需要将内部地图的替换功能更改为 Function.identity()并使用列表的大小提供比较器。外部地图的比较器仍然可以使用内部地图此时已经排序的事实,但也必须提取列表的 size()以进行比较。

We only have to change the inner map’s replacement function to Function.identity() and provide a comparator using the list’s sizes. The outer map’s comparator still can use the fact that the inner maps are already sorted at this point, but also has to extract the size() of the list for comparison.

这篇关于按嵌套映射的值对外部映射进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆