java 在 Hashmap<String, Object> 中对 Object 的值进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10214104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort the Object's value inside the Hashmap<String, Object>
提问by newbie
I want to sort an Hashmap by the object's value. In this case, by country code.
我想按对象的值对 Hashmap 进行排序。在这种情况下,按国家/地区代码。
KEY OBJECT
String LoyaltyCountry
- country name
- country code
- country loc
My code is as follows:
我的代码如下:
public static HashMap<String, LoyaltyCountry> loyaltyCountrySortMap(HashMap<String, LoyaltyCountry> loyaltyCountryMap) {
if (loyaltyCountryMap != null) {
List keys = new ArrayList();
keys.addAll(loyaltyCountryMap.keySet());
Collections.sort(keys, new Comparator<LoyaltyCountry>() {
public int compare(LoyaltyCountry o1, LoyaltyCountry o2) {
return o1.getCountryName().compareTo(o2.getCountryName());
}
});
}
return loyaltyCountryMap;
}
How can I do this correctly?
我怎样才能正确地做到这一点?
回答by Sarel Botha
Here is a method that returns a Set containing the Map.Entry items sorted by their value.
这是一个返回包含按其值排序的 Map.Entry 项的 Set 的方法。
public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> entriesSortedByValues(Map<K, V> map) {
SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(
new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
回答by trutheality
Create a Comparator<Map.Entry<String,LoyaltyCountry>>
, get the Map.entrySet()
, put it into a List<Map.Entry<String,LoyaltyCountry>>
and sort it.
创建 a Comparator<Map.Entry<String,LoyaltyCountry>>
,获取Map.entrySet()
,将其放入 aList<Map.Entry<String,LoyaltyCountry>>
并对其进行排序。
Clarification: You say you want to sort the HashMap
by values -- that isn't going to happen. A HashMap
is an unsorted implementation of a Map
. Moreover, the sorted implementations of Map
all sort on keys, so even a TreeMap
won't help you.
澄清:你说你想HashMap
按值排序- 这不会发生。AHashMap
是 a 的未排序实现Map
。此外,Map
所有排序键的排序实现,所以即使 aTreeMap
也无济于事。
The closest you can come to it (short of making your own map implementation) is produce a list of the map entries, which is what I described how to do above.
最接近它的方法(没有制作自己的地图实现)是生成地图条目的列表,这就是我在上面描述的如何执行的操作。
回答by Alexander Pogrebnyak
You can't sort HashMap
.
你不能排序HashMap
。
Looks like you want to return an ordered list of map's values, sorted by country name.
看起来您想要返回按国家/地区名称排序的地图值的有序列表。
For that change return type of your method to List, e.g
对于将您的方法的返回类型更改为 List,例如
public static List<LoyaltyCountry> loyaltyCountrySortMap(
HashMap<String, LoyaltyCountry> loyaltyCountryMap
)
{
if (loyaltyCountryMap != null) {
List<LoyaltyCountry> values = new ArrayList();
values.addAll(loyaltyCountryMap.values());
Collections.sort(values, new Comparator<LoyaltyCountry>() {
public int compare(LoyaltyCountry o1, LoyaltyCountry o2) {
return o1.getCountryName().compareTo(o2.getCountryName());
}
});
return values;
}
return null;
}