Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中按值对映射进行排序_Java_Sorting_Map_Hashmap - Fatal编程技术网

在Java中按值对映射进行排序

在Java中按值对映射进行排序,java,sorting,map,hashmap,Java,Sorting,Map,Hashmap,我试图对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) {

我试图对
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;
    }
}
no suitable method found for sort(List<Entry<K,V>>)
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method Collections.<T#2>sort(List<T#2>) is not applicable
      (inferred type does not conform to declared bound(s)
        inferred: Entry<K,V>
        bound(s): Comparable<? super Entry<K,V>>)
  where K,V,T#1,T#2 are type-variables:
    K extends Object declared in method <K,V>sortMapByValue(Map<K,V>)
    V extends Comparable<? super V> declared in method <K,V>sortMapByValue(Map<K,V>)
    T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
    T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
Collections.sort(list, new Comparator<Map.Entry<K, V>>()
{
    @Override
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return (o1.getValue()).compareTo(o2.getValue());
    }
});

但是,我希望按照这种方式修复错误(避免使用这个匿名比较器)。如何修复该错误?

Map.Entry
不实现
compariable
,因此
Collections.sort(List)
无法知道应如何对条目进行排序。所以你必须提供一个比较器

但是由于您的
SortMapByValue
已经实现了Comparator接口,因此您可以简单地使用该类的实例:

Collections.sort(list, new SortMapByValue<>());
Collections.sort(列表,新的SortMapByValue());
还请注意,使用Java 8可以显著缩短代码长度:

public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> unsortedMap) {
    return unsortedMap.entrySet().stream()
            .sorted(comparing(Entry::getValue))
            .collect(toMap(Entry::getKey, Entry::getValue, (e1,e2) -> e1, LinkedHashMap::new));
}

公共静态您有一个
列表
条目
。类
条目
未实现
可比
。但是,
Collections.sort(…)
需要一个实现
可比性的
列表
。因此,您的代码无法编译,并且工作正常。工作的代码的问题是您想要使用不编译的代码,而不编译的代码的问题是您想要编译它。我没有弄错吗?您需要将比较器传递给Collections.sort,例如像Collections.sort(list,new SortMapByValue());可能的副本可能会被关闭,因为不清楚你在问什么,真的吗?:)