Можете ли вы попробовать ниже код?
public class HomeMain {
public static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
Set<Map.Entry<String, Integer>> set = wordMap.entrySet();
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
return list;
}
public static void main(String[] args) {
Map<Home, List<People>> ihm = new HashMap<Home, List<People>>();
ihm.put(new Home("Home"), Arrays.asList(new People(4),
new People(5),
new People(6)));
ihm.put(new Home("Home1"), Arrays.asList(new People(2),
new People(1),
new People(9)));
ihm.put(new Home("Home2"), Arrays.asList(new People(3),
new People(6),
new People(2)));
ihm.put(new Home("Home3"), Arrays.asList(new People(1),
new People(7),
new People(6)));
Map<String, Integer> newMap = new HashMap<String, Integer>();
Iterator<Map.Entry<Home, List<People>>> itr = ihm.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<Home, List<People>> entry = itr.next();
List<People> p = entry.getValue();
int totalPeople = 0;
for (People people : p) {
totalPeople += people.getNumberOfPeople();
}
newMap.put(entry.getKey().getName(), totalPeople);
}
Iterator<Map.Entry<String, Integer>> it1 = newMap.entrySet().iterator();
System.out.println("UnSorted map:");
while (it1.hasNext()) {
Map.Entry<String, Integer> entry = it1.next();
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
List<Map.Entry<String, Integer>> sortedList = sortByValue(newMap);
System.out.println("Sorted map:");
for (Map.Entry<String, Integer> entry : sortedList) {
System.out.println(entry.getKey() + " ====" + entry.getValue());
}
}}
public class Home {
String name;
public Home(String homename){
this.name=homename;
}}
public class People {
public int getNumberOfPeople() {
return numberOfPeople;
}
int numberOfPeople;
public People(int numOfPeople){
this.numberOfPeople=numOfPeople;
}}