对地图进行排序< Key,Value>按值(Java) [英] Sort a Map<Key, Value> by values (Java)
问题描述
我对Java比较陌生,经常发现我需要对值进行排序 Map< Key,Value>
。因为值不唯一,我发现自己将 keySet
转换为数组
,并通过数组排序,并使用自定义比较对与该键相关联的值进行排序。有更简单的方法吗?
I am relatively new to Java, and often find that I need to sort a Map<Key, Value>
on the values. Since the values are not unique, I find myself converting the keySet
into an array
, and sorting that array through array sort with a custom comparator that sorts on the value associated with the key. Is there an easier way?
推荐答案
这是一个友好的版本,你可以免费使用:
Here's a generic-friendly version you're free to use:
import java.util.*;
public class MapUtil
{
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, Map.Entry<K, V> o2 )
{
return (o1.getValue()).compareTo( o2.getValue() );
}
} );
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list)
{
result.put( entry.getKey(), entry.getValue() );
}
return result;
}
}
和一个相关的JUnit4测试,以我的话为它:
And an associated JUnit4 test so you don't have to take my word for it:
import java.util.*;
import org.junit.*;
public class MapUtilTest
{
@Test
public void testSortByValue()
{
Random random = new Random(System.currentTimeMillis());
Map<String, Integer> testMap = new HashMap<String, Integer>(1000);
for(int i = 0 ; i < 1000 ; ++i) {
testMap.put( "SomeString" + random.nextInt(), random.nextInt());
}
testMap = MapUtil.sortByValue( testMap );
Assert.assertEquals( 1000, testMap.size() );
Integer previous = null;
for(Map.Entry<String, Integer> entry : testMap.entrySet()) {
Assert.assertNotNull( entry.getValue() );
if (previous != null) {
Assert.assertTrue( entry.getValue() >= previous );
}
previous = entry.getValue();
}
}
}
版本
Java 7 Version
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.entrySet() );
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<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list)
{
result.put( entry.getKey(), entry.getValue() );
}
return result;
}
Java 8版本。这将根据值按升序排序;对于降序,只需取消对 Collections.reverseOrder()
的调用。
Java 8 Version. This will sort according to the value in ascending order; for descending order, it is just possible to uncomment the call to Collections.reverseOrder()
.
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
return map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(/*Collections.reverseOrder()*/))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
}
这篇关于对地图进行排序< Key,Value>按值(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!