0

I want to find 10 max values of a hashmap and i want others to be united into a single value called "Altri", in english it means others.

How can I do this?

I tried with Collections.max(collection) after defining Collection collection=map.values() but it doesn't work. When I debug it, I see it takes one value that it isn't the higher.

Can anyone help me? Thank's in advance

4

2 回答 2

0
Map<T, T> map = new TreeMap<T, T>(yourMap);

它会给你排序的数据。(自然排序1,2,3....100)

然后从地图中读取最后 10 个值。

您将获得 10 个 MAX 值

或使用

SortedMap<T,T> instead of Map

它将返回排序后的数据

于 2012-09-20T10:31:26.993 回答
0

为此,您必须像这样遍历哈希图:

Map.Entry<Foo, Bar> maxEntry = null;

    for (Map.Entry<Foo, Bar> entry : map.entrySet())
    {
        if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
        {
            maxEntry = entry;
        }
    }
于 2012-09-20T10:33:31.283 回答