按值对Map <Key,Value>进行排序
我对Java比较陌生,经常发现我需要对这些值排序一个" Map <Key,Value>"。
由于值不是唯一的,因此我发现自己将keySet转换为array,并使用自定义比较器对数组进行排序,该自定义比较器对与键关联的值进行排序。
有没有更简单的方法?
解决方案
从http://www.programmersheaven.com/download/49349/download.aspx
private static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());
}
});
Map<K, V> result = new LinkedHashMap<>();
for (Iterator<Entry<K, V>> it = list.iterator(); it.hasNext();) {
Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
根据上下文,使用java.util.LinkedHashMap <T>来记住项目在地图中的放置顺序。否则,如果我们需要根据自然顺序对值进行排序,我建议我们维护一个单独的List,该列表可以通过Collections.sort()进行排序。
commons-collections库包含一个名为TreeBidiMap的解决方案。或者,我们可以查看Google Collections API。它具有TreeMultimap,我们可以使用。
而且,如果我们不想使用这些框架,它们都带有源代码。
如果Map值实现Comparable(例如String),则此方法应该可以正常工作
Map<Object, String> map = new HashMap<Object, String>(); // Populate the Map List<String> mapValues = new ArrayList<String>(map.values()); Collections.sort(mapValues);
如果地图值本身未实现Comparable,但是我们有一个Comparable实例可以对它们进行排序,则用以下内容替换最后一行:
Collections.sort(mapValues, comparable);
为了对键进行排序,我找到了一个更好的TreeMap解决方案(我也将尝试为基于值的排序准备好解决方案):
public static void main(String[] args) {
Map<String, String> unsorted = new HashMap<String, String>();
unsorted.put("Cde", "Cde_Value");
unsorted.put("Abc", "Abc_Value");
unsorted.put("Bcd", "Bcd_Value");
Comparator<String> comparer = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}};
Map<String, String> sorted = new TreeMap<String, String>(comparer);
sorted.putAll(unsorted);
System.out.println(sorted);
}
输出为:
{Abc = Abc_Value,Bcd = Bcd_Value,Cde = Cde_Value}
使用java.util.TreeMap。
"根据地图的键的自然顺序或者在地图创建时通过提供的Comparator来对地图进行排序,具体取决于所使用的构造函数。"
对键进行排序要求比较器为每个比较查找每个值。更具可扩展性的解决方案将直接使用entrySet,因为此后该值将立即可用于每个比较(尽管我尚未通过数字对其进行备份)。
这是这种事情的通用版本:
public static <K, V extends Comparable<? super V>> List<K> getKeysSortedByValue(Map<K, V> map) {
final int size = map.size();
final List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(size);
list.addAll(map.entrySet());
final ValueComparator<V> cmp = new ValueComparator<V>();
Collections.sort(list, cmp);
final List<K> keys = new ArrayList<K>(size);
for (int i = 0; i < size; i++) {
keys.set(i, list.get(i).getKey());
}
return keys;
}
private static final class ValueComparator<V extends Comparable<? super V>>
implements Comparator<Map.Entry<?, V>> {
public int compare(Map.Entry<?, V> o1, Map.Entry<?, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}
对于上述解决方案,有一些方法可以减少内存旋转。例如,创建的第一个ArrayList可以重新用作返回值;这将需要抑制一些泛型警告,但是对于可重用的库代码可能是值得的。同样,不必在每次调用时都重新分配比较器。
这是一个效率更高的版本,尽管吸引力不大:
public static <K, V extends Comparable<? super V>> List<K> getKeysSortedByValue2(Map<K, V> map) {
final int size = map.size();
final List reusedList = new ArrayList(size);
final List<Map.Entry<K, V>> meView = reusedList;
meView.addAll(map.entrySet());
Collections.sort(meView, SINGLE);
final List<K> keyView = reusedList;
for (int i = 0; i < size; i++) {
keyView.set(i, meView.get(i).getKey());
}
return keyView;
}
private static final Comparator SINGLE = new ValueComparator();
最后,如果我们需要连续访问排序后的信息(而不是仅偶尔进行排序),则可以使用其他多图。如果我们需要更多详细信息,请告诉我...
好的,此版本可用于两个新的Map对象以及两个迭代和值排序。希望,尽管映射项必须循环两次,但效果还是不错的:
public static void main(String[] args) {
Map<String, String> unsorted = new HashMap<String, String>();
unsorted.put("Cde", "Cde_Value");
unsorted.put("Abc", "Abc_Value");
unsorted.put("Bcd", "Bcd_Value");
Comparator<String> comparer = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}};
System.out.println(sortByValue(unsorted, comparer));
}
public static <K, V> Map<K,V> sortByValue(Map<K, V> in, Comparator<? super V> compare) {
Map<V, K> swapped = new TreeMap<V, K>(compare);
for(Entry<K,V> entry: in.entrySet()) {
if (entry.getValue() != null) {
swapped.put(entry.getValue(), entry.getKey());
}
}
LinkedHashMap<K, V> result = new LinkedHashMap<K, V>();
for(Entry<V,K> entry: swapped.entrySet()) {
if (entry.getValue() != null) {
result.put(entry.getValue(), entry.getKey());
}
}
return result;
}
该解决方案将TreeMap与Comparator结合使用,并对所有空键和值进行分类。首先,使用TreeMap的排序功能对值进行排序,然后将排序后的Map用于创建结果,因为保留的LinkedHashMap具有相同的值顺序。
加的特Greetz
遇到这个问题时,我只是在侧面创建了一个列表。如果将它们放在一起放在自定义的Map实现中,将会有很好的感觉……我们可以使用以下类似内容,仅在需要时执行排序。 (注意:我还没有真正测试过它,但是它可以编译...在某处可能是一个愚蠢的小错误)
(如果要按键和值对它进行排序,请让该类扩展TreeMap,不要定义访问器方法,并让mutators调用super.xxxxx而不是map_.xxxx)
package com.javadude.sample;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SortedValueHashMap<K, V> implements Map<K, V> {
private Map<K, V> map_ = new HashMap<K, V>();
private List<V> valueList_ = new ArrayList<V>();
private boolean needsSort_ = false;
private Comparator<V> comparator_;
public SortedValueHashMap() {
}
public SortedValueHashMap(List<V> valueList) {
valueList_ = valueList;
}
public List<V> sortedValues() {
if (needsSort_) {
needsSort_ = false;
Collections.sort(valueList_, comparator_);
}
return valueList_;
}
// mutators
public void clear() {
map_.clear();
valueList_.clear();
needsSort_ = false;
}
public V put(K key, V value) {
valueList_.add(value);
needsSort_ = true;
return map_.put(key, value);
}
public void putAll(Map<? extends K, ? extends V> m) {
map_.putAll(m);
valueList_.addAll(m.values());
needsSort_ = true;
}
public V remove(Object key) {
V value = map_.remove(key);
valueList_.remove(value);
return value;
}
// accessors
public boolean containsKey(Object key) { return map_.containsKey(key); }
public boolean containsValue(Object value) { return map_.containsValue(value); }
public Set<java.util.Map.Entry<K, V>> entrySet() { return map_.entrySet(); }
public boolean equals(Object o) { return map_.equals(o); }
public V get(Object key) { return map_.get(key); }
public int hashCode() { return map_.hashCode(); }
public boolean isEmpty() { return map_.isEmpty(); }
public Set<K> keySet() { return map_.keySet(); }
public int size() { return map_.size(); }
public Collection<V> values() { return map_.values(); }
}
尽管我同意对地图进行排序的持续需求可能是一种气味,但我认为以下代码是在不使用其他数据结构的情况下最简单的方法。
public class MapUtilities {
public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValue(Map<K, V> map) {
List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet());
Collections.sort(entries, new ByValue<K, V>());
return entries;
}
private static class ByValue<K, V extends Comparable<V>> implements Comparator<Entry<K, V>> {
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}
}
这是一个令人尴尬的不完整的单元测试:
public class MapUtilitiesTest extends TestCase {
public void testSorting() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
List<Map.Entry<String, Integer>> sorted = MapUtilities.sortByValue(map);
assertEquals("First", "One", sorted.get(0).getKey());
assertEquals("Second", "Two", sorted.get(1).getKey());
assertEquals("Third", "Three", sorted.get(2).getKey());
}
}
结果是Map.Entry对象的排序列表,从中可以获取键和值。
基于@devinmoore代码,这是一种使用泛型并支持升序和降序排序的地图排序方法。
/**
* Sort a map by it's keys in ascending order.
*
* @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
* @author Maxim Veksler
*/
public static <K, V> LinkedHashMap<K, V> sortMapByKey(final Map<K, V> map) {
return sortMapByKey(map, SortingOrder.ASCENDING);
}
/**
* Sort a map by it's values in ascending order.
*
* @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
* @author Maxim Veksler
*/
public static <K, V> LinkedHashMap<K, V> sortMapByValue(final Map<K, V> map) {
return sortMapByValue(map, SortingOrder.ASCENDING);
}
/**
* Sort a map by it's keys.
*
* @param sortingOrder {@link SortingOrder} enum specifying requested sorting order.
* @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
* @author Maxim Veksler
*/
public static <K, V> LinkedHashMap<K, V> sortMapByKey(final Map<K, V> map, final SortingOrder sortingOrder) {
Comparator<Map.Entry<K, V>> comparator = new Comparator<Entry<K,V>>() {
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return comparableCompare(o1.getKey(), o2.getKey(), sortingOrder);
}
};
return sortMap(map, comparator);
}
/**
* Sort a map by it's values.
*
* @param sortingOrder {@link SortingOrder} enum specifying requested sorting order.
* @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
* @author Maxim Veksler
*/
public static <K, V> LinkedHashMap<K, V> sortMapByValue(final Map<K, V> map, final SortingOrder sortingOrder) {
Comparator<Map.Entry<K, V>> comparator = new Comparator<Entry<K,V>>() {
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return comparableCompare(o1.getValue(), o2.getValue(), sortingOrder);
}
};
return sortMap(map, comparator);
}
@SuppressWarnings("unchecked")
private static <T> int comparableCompare(T o1, T o2, SortingOrder sortingOrder) {
int compare = ((Comparable<T>)o1).compareTo(o2);
switch (sortingOrder) {
case ASCENDING:
return compare;
case DESCENDING:
return (-1) * compare;
}
return 0;
}
/**
* Sort a map by supplied comparator logic.
*
* @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.
* @author Maxim Veksler
*/
public static <K, V> LinkedHashMap<K, V> sortMap(final Map<K, V> map, final Comparator<Map.Entry<K, V>> comparator) {
// Convert the map into a list of key,value pairs.
List<Map.Entry<K, V>> mapEntries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
// Sort the converted list according to supplied comparator.
Collections.sort(mapEntries, comparator);
// Build a new ordered map, containing the same entries as the old map.
LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(map.size() + (map.size() / 20));
for(Map.Entry<K, V> entry : mapEntries) {
// We iterate on the mapEntries list which is sorted by the comparator putting new entries into
// the targeted result which is a sorted map.
result.put(entry.getKey(), entry.getValue());
}
return result;
}
/**
* Sorting order enum, specifying request result sort behavior.
* @author Maxim Veksler
*
*/
public static enum SortingOrder {
/**
* Resulting sort will be from smaller to biggest.
*/
ASCENDING,
/**
* Resulting sort will be from biggest to smallest.
*/
DESCENDING
}
重要的提示:
该代码可以以多种方式破坏。如果我们打算使用提供的代码,请务必阅读注释,并注意其中的含义。例如,无法再通过键检索值。 (get总是返回null。)
似乎比上述所有内容都容易得多。使用TreeMap如下:
public class Testing {
public static void main(String[] args) {
HashMap<String, Double> map = new HashMap<String, Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);
map.put("A", 99.5);
map.put("B", 67.4);
map.put("C", 67.4);
map.put("D", 67.3);
System.out.println("unsorted map: " + map);
sorted_map.putAll(map);
System.out.println("results: " + sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with
// equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
输出:
unsorted map: {D=67.3, A=99.5, B=67.4, C=67.4}
results: {D=67.3, B=67.4, C=67.4, A=99.5}
我已经看了给出的答案,但是其中许多答案比所需的更为复杂,或者当多个键具有相同的值时删除映射元素。
这是我认为更合适的解决方案:
public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
}
};
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
请注意,地图是从最高值到最低值排序的。
这是通用的版本:
public class MapUtil {
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
list.sort(Entry.comparingByValue());
Map<K, V> result = new LinkedHashMap<>();
for (Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
这太复杂了。地图不应执行按值对地图进行排序的工作。最简单的方法是创建自己的类,使其适合要求。
在下面的示例中,我们应该在*所在的位置添加TreeMap一个比较器。但是通过Java API,它仅向比较器提供键,而不是值。此处陈述的所有示例均基于2个地图。一哈希和一棵新树。真奇怪
这个例子:
Map<Driver driver, Float time> map = new TreeMap<Driver driver, Float time>(*);
因此,可以通过以下方式将地图更改为集合:
ResultComparator rc = new ResultComparator(); Set<Results> set = new TreeSet<Results>(rc);
我们将创建类"结果",
public class Results {
private Driver driver;
private Float time;
public Results(Driver driver, Float time) {
this.driver = driver;
this.time = time;
}
public Float getTime() {
return time;
}
public void setTime(Float time) {
this.time = time;
}
public Driver getDriver() {
return driver;
}
public void setDriver (Driver driver) {
this.driver = driver;
}
}
和Comparator类:
public class ResultsComparator implements Comparator<Results> {
public int compare(Results t, Results t1) {
if (t.getTime() < t1.getTime()) {
return 1;
} else if (t.getTime() == t1.getTime()) {
return 0;
} else {
return -1;
}
}
}
这样,我们可以轻松添加更多依赖项。
最后,我将添加简单的迭代器:
Iterator it = set.iterator();
while (it.hasNext()) {
Results r = (Results)it.next();
System.out.println( r.getDriver().toString
//or whatever that is related to Driver class -getName() getSurname()
+ " "
+ r.getTime()
);
}
三个1行答案...
如果值是"可比较的",我会使用Google Collections Guava来做到这一点,那么我们可以使用
valueComparator = Ordering.natural().onResultOf(Functions.forMap(map))
它将为地图创建一个函数(对象)(将任何键作为输入,返回各自的值),然后对它们(值)应用自然的(可比较的)排序。
如果它们不具有可比性,那么我们需要按照以下步骤进行操作
valueComparator = Ordering.from(comparator).onResultOf(Functions.forMap(map))
这些可以应用于TreeMap(由于Ordering扩展Comparator)或者经过某种排序的LinkedHashMap
注意:如果要使用TreeMap,请记住,如果比较== 0,则该项目已经在列表中(如果我们有多个比较相同的值,则会发生此情况)。为了减轻这种情况,我们可以像这样将键添加到比较器中(假设键和值是"可比较的"):
valueComparator = Ordering.natural().onResultOf(Functions.forMap(map)).compound(Ordering.natural())
=将自然顺序应用于键所映射的值,并将其与键的自然顺序进行复合
请注意,如果键比较为0,这仍然不起作用,但是对于大多数"可比较"项来说已经足够了(因为" hashCode"," equals"和" compareTo"通常是同步的...)
请参阅Ordering.onResultOf()和Functions.forMap()。
执行
因此,现在我们有了一个可以完成我们想要的操作的比较器,我们需要从中获取结果。
map = ImmutableSortedMap.copyOf(myOriginalMap, valueComparator);
现在,这很可能会起作用,但是:
- 需要完成完整的地图
- 不要在TreeTree上尝试上面的比较器;在插入之前没有值的情况下尝试比较插入的键是没有意义的,即,它将很快中断
第一点对我来说有点麻烦。谷歌收藏集非常懒惰(这很好:我们可以立即执行几乎所有操作;当我们开始使用结果时,实际工作就完成了),这需要复制整个地图!
"完整"答案/按值实时排序的地图
不过不要担心。如果我们沉迷于以这种方式对"实时"地图进行排序,则可以通过以下类似的疯狂方法解决上述两个问题,而不是解决其中一个:
注意:这种情况在2012年6月发生了重大变化,以前的代码永远无法使用:需要内部HashMap来查找值,而无需在TreeMap.get()-> compare()和compare( )->get()
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import com.google.common.base.Functions;
import com.google.common.collect.Ordering;
class ValueComparableMap<K extends Comparable<K>,V> extends TreeMap<K,V> {
//A map for doing lookups on the keys for comparison so we don't get infinite loops
private final Map<K, V> valueMap;
ValueComparableMap(final Ordering<? super V> partialValueOrdering) {
this(partialValueOrdering, new HashMap<K,V>());
}
private ValueComparableMap(Ordering<? super V> partialValueOrdering,
HashMap<K, V> valueMap) {
super(partialValueOrdering //Apply the value ordering
.onResultOf(Functions.forMap(valueMap)) //On the result of getting the value for the key from the map
.compound(Ordering.natural())); //as well as ensuring that the keys don't get clobbered
this.valueMap = valueMap;
}
public V put(K k, V v) {
if (valueMap.containsKey(k)){
//remove the key in the sorted set before adding the key again
remove(k);
}
valueMap.put(k,v); //To get "real" unsorted values for the comparator
return super.put(k, v); //Put it in value order
}
public static void main(String[] args){
TreeMap<String, Integer> map = new ValueComparableMap<String, Integer>(Ordering.natural());
map.put("a", 5);
map.put("b", 1);
map.put("c", 3);
assertEquals("b",map.firstKey());
assertEquals("a",map.lastKey());
map.put("d",0);
assertEquals("d",map.firstKey());
//ensure it's still a map (by overwriting a key, but with a new value)
map.put("d", 2);
assertEquals("b", map.firstKey());
//Ensure multiple values do not clobber keys
map.put("e", 2);
assertEquals(5, map.size());
assertEquals(2, (int) map.get("e"));
assertEquals(2, (int) map.get("d"));
}
}
放置时,请确保哈希图具有比较器的值,然后将其放入TreeSet进行排序。但在此之前,我们检查哈希图以查看该密钥实际上不是重复项。另外,我们创建的比较器还将包含键,以便重复值不会删除非重复键(由于==比较)。
这两个项目对于确保遵守地图合同至关重要。如果我们认为自己不想这么做,那么我们几乎就要完全反转地图了(到Map <V,K>)。
构造函数需要被称为
new ValueComparableMap(Ordering.natural()); //or new ValueComparableMap(Ordering.from(comparator));

