地图中的地图条目列表

时间:2018-04-12 12:38:47

标签: java list

我试图从地图中获取(按值排序)地图条目列表。我试过这个:

zeppelin.interpreter.localRepo

但如果我尝试将Pair替换为Map.Entry,它会告诉我Map.Entry是抽象的,无法实例化。有没有办法调整这个构造来获取条目列表而不是对列表?

2 个答案:

答案 0 :(得分:4)

请参阅Javadoc for Map.Entry

  

接口Map.Entry

     

所有已知的实施课程:
  AbstractMap.SimpleEntryAbstractMap.SimpleImmutableEntry

选择一个适合您需求的实施课程,例如

List<Entry<String, AtomicInteger>> expectedList = expectedMap.entrySet().stream()
            .sorted(Comparator.comparingInt(e -> -e.getValue().get()))
            .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue()))
            .collect(Collectors.toList());

但您也可以删除map并使用entrySet()返回的条目:

List<Entry<String, AtomicInteger>> expectedList = expectedMap.entrySet().stream()
            .sorted(Comparator.comparingInt(e -> -e.getValue().get()))
            .collect(Collectors.toList());

PS。:如果要比较原始数据类型,则应使用正确的方法,如comparingInt。这避免了不必要的拳击。

答案 1 :(得分:1)

如果您需要Map.Entry,我不明白为什么您需要将List<Map.Entry<String, AtomicInteger>> expectedList = new ArrayList<>(expectedMap.entrySet()); Collections.sort(expectedList, Map.Entry.comparingByValue( Comparator.comparingInt(AtomicInteger::get).reversed()); 映射到其他内容。

这是我的看法。我选择先创建一个条目列表然后对其进行排序。还使用内置API的mor来构造比较器。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

<IfModule mime_module>
  AddType application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
相关问题