通用方法对值进行排序

通用方法对值进行排序

本文介绍了通用方法对值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,应该根据它的值对地图进行排序。这是我迄今为止:

  public static  sortByValues(final Map< K,V> mapToSort)
{
List< Map.Entry< K,V>>条目=
新的ArrayList< Map.Entry< K,V>>(mapToSort.size());

entries.addAll(mapToSort.entrySet());

Collections.sort(entries,new Comparator< Map.Entry< K,V>>()
{
public int compare(
final Map.Entry< ; K,V> entry1,
final Map.Entry< K,V> entry2)
{
return entry1.getValue()。compareTo(entry2.getValue());
}
});

地图< K,V> sortedMap = new LinkedHashMap< K,V>(); (Map.Entry< K,V> entry:entries)


{
sortedMap.put(entry.getKey(),entry.getValue());
}

return sortedMap;
}

我希望我的通用值V与任何 V或者至少是V的一个子类



我得到以下错误代码片段:

  public静态  



如何更具限制性?



如果我将声明更改为:

  public static   

然后就没有错误了。但是,这不是我想要的。



我的一个解决方法是,我可以将声明更改为:

  public static< K,V extends Comparable< V>> 

但是我这样做失去了灵活性,我无法传递一个Map,它的值实现了Comparable和一个子类本身。



对于如此长的问题,道歉。

解决方案

我认为你的第二个选择,即

是。我认为这是因为当你写了

你基本上说你希望能够 V 的任何实例与 C 的任何实例进行比较。但是这里缺少的是,因为你想在内部调用 Collections.sort(..),所以你也必须能够比较任何 C 添加到 V 的任何实例。但是泛型没有表达这一点,并且正确地说编译器会抱怨。

基本上,要对一些值进行排序(至少使用 Collections.sort(。 。))它们必须互相可比,但是您设想的通用限制只能保证您可以在一个方向上进行比较。


I have a small program that is supposed to sort a map based on its values. Here is what I have so far:

    public static <K, V extends Comparable< ? extends V>> Map<K, V>
    sortByValues(final Map <K, V> mapToSort)
    {
        List<Map.Entry<K, V>> entries =
            new ArrayList<Map.Entry<K, V>>(mapToSort.size());

        entries.addAll(mapToSort.entrySet());

        Collections.sort(entries, new Comparator<Map.Entry<K, V>>()
        {
            public int compare(
                               final Map.Entry<K, V> entry1,
                               final Map.Entry<K, V> entry2)
            {
                return entry1.getValue().compareTo(entry2.getValue());
            }
        });

        Map<K, V> sortedMap = new LinkedHashMap<K, V>();

        for (Map.Entry<K, V> entry : entries)
        {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }

I want my generic value V to be comparable to anything that is either V or is a at least a subclass of V.

I get the following error for the code piece :

public static <K, V extends Comparable< ? extends V>>

How can it be more restrictive?

If I change the declaration to:

public static <K, V extends Comparable< ? super V>>

then there is no error. But this is not what I want.

One workaround I have is that, I can change the declaration to:

public static <K, V extends Comparable<V>>

but doing this I lose the flexibility in that I cannot pass a Map whose value implements Comparable with a subclass of itself.

Apologies for such a long question. Thanks in advance.

解决方案

I think your second option, namely

is the way to. I think this is so because when you write

you basically say that you want to be able to compare any instance of V to any instance of C. But what is missing here is, that because you want to call Collections.sort(..) internally, you also must be able to compare any instance of C to any instance of V. But the generics do not express this, and rightfully the compiler complains.

Basically, to sort some values (at least using Collections.sort(..)) they must be mutually comparable, but the generic restrictions you envision only guarantee that you can compare in one direction.

这篇关于通用方法对值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:20