简体   繁体   English

排序地图 <String, Integer> 最大的先入名单 <String> 有效率的?

[英]Sort Map<String, Integer> Largest First Into List<String> Efficiently?

I have a ConcurrentMap<String, Integer> , and I'd like to get a List<String> from it with the String mapping to the largest Integer first, second largest second, etc. 我有一个ConcurrentMap<String, Integer> ,我想从它获得一个List<String> ,其中String映射到最大的Integer第一,第二大,等等。

Right now I have something along the lines of this: 现在我有一些类似的东西:
Loop through the keySet of the map 循环遍历地图的keySet
In that loop, loop through a "sorted" List<String> 在该循环中,循环遍历“已排序” List<String>
Keep looping until the key String's respective value is less than element i of the "sorted" List , and insert it. 保持循环,直到键字符串的相应值小于“已排序” List元素i ,然后插入它。

Now this will work, but I doubt it's very efficient. 现在这将有效,但我怀疑它是非常有效的。 Does Java 8 have any built in sorting algorithms that could help me here? Java 8是否有任何内置的排序算法可以帮助我?

Using streams, you could write it like this: 使用流,你可以像这样写:

List<String> sorted = map.entrySet().stream()
                         .sorted(reverseOrder(comparing(Entry::getValue)))
                         .map(Entry::getKey)
                         .collect(toList());

Or as commented by Holger: 或者如Holger评论的那样:

List<String> sorted = map.entrySet().stream()
                         .sorted(comparingByValue(reverseOrder()))
                         .map(Entry::getKey)
                         .collect(toList());

Note static imports: 注意静态导入:

import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

I don't know if that's more efficient than your method but you can profile both and decide based on actual measurement. 我不知道这是否比你的方法更有效,但你可以分析两者并根据实际测量结果来决定。

Yes, it does. 是的,它确实。 You could do something like this: 你可以这样做:

ConcurrentMap<String, Integer> map = /* something */;

// entrySet just returns a view of the entries, so copy it into a new List
List<Map.Entry<String, Integer>> entries = new ArrayList<>(yourMap.entrySet());

// sort entries by their int values
Collections.sort(entries, (entry1, entry2) -> Integer.compare(entry1.getValue(), entry2.getValue()));

// copy just the keys into a new List
List<String> result = new LinkedList<>();
for (Map.Entry<String, Integer> entry : entries) {
  result.add(entry.getKey());
}
import java.util. TreeSet;

import java.util.Set; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;

public class Test { 公共课测试{

public static void main(String[] args){
    ConcurrentHashMap< String, Integer> map = new ConcurrentHashMap<String , Integer>();
    map.put("Fish", 1);
    map.put("Bird", 100);
    map.put("Reptile", 2);
    map.put("Mammal", 10);
    TreeSet <Integer> sortedList = new TreeSet <Integer>(map.values());
    for(Integer i :sortedList){
        System.out.println(i);
    }
}

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 排序地图列表 <String, String> 基于整数 - sort a List of Map<String, String> based integer value 排序列表<map.entry<string, integer> &gt; 主要和次要方式? </map.entry<string,> - Sort List<Map.Entry<String, Integer>> in primary and secondary way? 展平地图<Integer, List<String> &gt; 到地图<String, Integer>使用流和 lambda - Flatten a Map<Integer, List<String>> to Map<String, Integer> with stream and lambda 如何转换地图 <Integer, List<String> &gt;到地图 <Integer, Set<String> &gt; - how to convert Map<Integer, List<String>> to Map<Integer, Set<String>> Java List <String>到Map <String,Integer> convertion - Java List<String> to Map<String, Integer> convertion 转换列表<string>至 Map<string, integer></string,></string> - Convert List<String> to Map<String, Integer> android-如何排序列表 <Map<String, String> &gt; - android - How to sort List<Map<String, String>> 转换 Map <integer, list<strings> 至 Map <string, list<integer></string,></integer,> - Convert Map<Integer, List<Strings> to Map<String, List<Integer> 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? 对具有 2 个道具的对象列表进行排序,即字符串和整数,按字母顺序对此列表进行排序,如果字符串相同,则按 DESC 整数排序 - Sort a list of object with 2 props are String and Integer, sort this list by alphabetical and if the String is the same, sort by DESC Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM