Sort the Object's value inside the Hashmap<String, Object>
Solution 1
Here is a method that returns a Set containing the Map.Entry items sorted by their value.
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;
}
Solution 2
Create a Comparator<Map.Entry<String,LoyaltyCountry>>
, get the Map.entrySet()
, put it into a List<Map.Entry<String,LoyaltyCountry>>
and sort it.
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.
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.
Solution 3
You can't sort 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
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;
}
newbie
I'm a newbie, a novice, an amateur, a beginner in programming. My Total Programming Experience is approximately equal to the no. of months I joined here! So pardon me for my idiotic questions. My dream is to be a great programmer someday. To make the impossible, possible. I want to be the female version of Mr. Jon Skeet and my idol (who always scolds me hehehe) Mr Balusc. But I think, based on my current capabilities, i need a whooping 50 years to be on their level. So help me God! BTW, i really find this site super cool! I could interact and learn from many great programmers around the world. And for a programmer-wannabe like me, every opinion matters. What's important is that I'm learning a lot here which I could not learn by just reading books. So thank you everyone for all the help. I think I am becoming a stackoverflow addict.
Updated on June 14, 2022Comments
-
newbie almost 2 years
I want to sort an Hashmap by the object's value. In this case, by country code.
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?