我正在尝试排序java.util.Map如下.
public final class SortMapByValue <K, V extends Comparable<? super V>> implements Comparator<Map.Entry<K, V>>
{
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> unsortedMap)
{
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(unsortedMap.entrySet());
Collections.sort(list); //Compiler error here.
Map<K, V> sortedMap = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
Run Code Online (Sandbox Code Playgroud)
如代码中所述,它发出编译时错误,如下所示.
no suitable method …Run Code Online (Sandbox Code Playgroud) 在Java中,按照它们出现在列表中的次数对大量单词列表(10,000-20,000)进行排序的最佳/最简单方法是什么?我尝试了一个基本的实现,但是我得到了内存运行时错误,所以我需要一种更有效的方法.你会建议什么?
ArrayList<String> occuringWords = new ArrayList<String>();
ArrayList<Integer> numberOccur = new ArrayList<Integer>();
String temp;
int count;
for(int i = 0; i < finalWords.size(); i++){
temp = finalWords.get(i);
count = 0;
for(int j = 0; j < finalWords.size(); j++){
if(temp.equals(finalWords.get(j))){
count++;
finalWords.remove(j);
j--;
}
}
if(numberOccur.size() == 0){
numberOccur.add(count);
occuringWords.add(temp);
}else{
for(int j = 0; j < numberOccur.size(); j++){
if(count>numberOccur.get(j)){
numberOccur.add(j, count);
occuringWords.add(j, temp);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
finalWords是所有字符串的列表.我必须存储每个单词出现在单独的arraylist中的次数,因为我想不出更好的方法来保持它们配对而不将每个单词作为单独的对象.
我正在查看一个冗余的字符串列表,例如
{ "One", "One", "One", "Two", "Three", "Three" }
Run Code Online (Sandbox Code Playgroud)
计算出现次数的最佳方法是什么,然后创建一个非冗余的字符串列表,按出现次数排序?
我想要的结果是这样的List:
{ "One", "Three", "Two" }
Run Code Online (Sandbox Code Playgroud) 我有一个HashMap:
private Map<String,Integer> matchesWonByTeam= new HashMap<String,Integer>();
Run Code Online (Sandbox Code Playgroud)
使用集合和比较器的最简单和最简单的方法是什么?
Stream<Map.Entry<String, List<Object>>> sorted = index.entrySet().stream()
.sorted(Map.Entry.comparingByValue());
Run Code Online (Sandbox Code Playgroud)
sorted(Comparator<? super Map.Entry<String,List<NFArticle>>>)类型中的方法Stream<Map.Entry<String,List<NFArticle>>>不适用于参数(Comparator <Map.Entry<Object,Comparable<? super Comparable<? super V>>>>)
我想Hashmap根据size()列表中的值来排序HashMap.如何使用Java 8中的Stream库实现此目的?
private static <K, V extends Comparable<? super V>> Map<K, V>
sortByValue( Map<K, V> map )
{
Map<K, V> result = new LinkedHashMap<>();
Stream<Map.Entry<K, V>> st = map.entrySet().stream();
st.sorted( Map.Entry.comparingByValue() )
.forEachOrdered( e -> result.put(e.getKey(), e.getValue()) );
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是这篇文章的一个例子.有用.问题是它按升序排序.如何将其更改为降序?
我可以这样做:
public static <K, V extends Comparable<? super V>> Map<K, V>
sortByValue( Map<K, V> map )
{
List<Map.Entry<K, V>> list =
new LinkedList<Map.Entry<K, V>>( map.entrySet() );
Collections.sort( list, new Comparator<Map.Entry<K, V>>()
{
public int compare( Map.Entry<K, V> o1, …Run Code Online (Sandbox Code Playgroud) 这是我的json:
[
{"name": "John Smith","date":"2017-02-02T23:07:09Z","id":"1223234"}
{"name": "John Doe","date":"2015-07-03T21:05:10Z","id":"3242342"},
{"name": "Jane Fan","date":"2016-12-22T13:27:19Z","id":"2123444"}
]
Run Code Online (Sandbox Code Playgroud)
我已将上面的内容转换为hashmap.现在我想对日期值进行排序.
是否有内置功能可以帮助我实现这一目标?例如,用Python排序
所以我是新手,我在地图上挣扎很多。所以我有一张地图,我想按按键升序排序。但是我似乎找不到任何解决方案。谢谢您的帮助!
LinkedHashMap<String, String> resulting= new LinkedHashMap<>();
resulting=resulting.entrySet().stream().sorted((a,b)->{
String first=a.getKey();
String second=b.getKey();
return first.compareTo(second)
});
Run Code Online (Sandbox Code Playgroud) 我有一个HashMap,我需要按值排序,我试图保持简洁,所以我使用Java 8.但是各种方法都不起作用,我不确定为什么.我试过这个:
followLikeCount.values()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这会抛出这个编译时异常:
Main.java:65: error: no suitable method found for sorted(Comparator<Entry<Object,V#1>>)
.sorted(Map.Entry.comparingByValue())
Run Code Online (Sandbox Code Playgroud)
我不明白为什么观察不匹配.我也尝试过使用比较器:
Comparator<Map.Entry<Integer, Integer>> byValue =
Map.Entry.<Integer, Integer>comparingByValue();
Run Code Online (Sandbox Code Playgroud)
这会产生类似的错误.请问您能告知为什么比较器无效?
java ×10
sorting ×7
hashmap ×3
java-8 ×3
dictionary ×2
arraylist ×1
collections ×1
comparator ×1
counting ×1
map ×1
parsing ×1