[英]Java sort map based on count of value of list
例如
Map<Home, List<People>> ihm = new TreeMap<Home, List<People>>();
数据如下:
ihm.put(new Home(...), Arrays.asList(new People(...),
new People(...),
new People(...));
ihm.put(new Home(...), Arrays.asList(new People(...),
new People(...));
我想按编号排序。 住在房子里的人。
我如何使用比较器或比较器来实现这一点?
它不应该作为地图的关键 Home 的属性来完成,因为您可能想要向 Home 添加/删除人员,从而损坏地图。
而是动态排序:
ihm.entrySet().stream()
.sort(Comparator.comparingInt(es -> -es.getValue().size())) // Decreasing; neg. sizes.
.forEach(es -> System.out.printf("...%n", ...));
你可以试试下面的代码吗?
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;
}}
如何做到这一点的一个例子。
public static void main(String[] args){
Map<Home, List<People>> ihm = new HashMap<Home, List<People>>();
ihm.put(new Home(3), Arrays.asList(new People(1), new People(2),new People(3)));
ihm.put(new Home(2), Arrays.asList(new People(1), new People(4)));
ihm.put(new Home(4), Arrays.asList(new People(5), new People(4),new People(2),new People(3)));
ihm.put(new Home(1), Collections.singletonList(new People(5)));
System.out.println("\nUnSorted Map :");
for (Map.Entry<Home, List<People>> entry:ihm.entrySet()) {
System.out.println(entry.getKey());
}
Map<Home,List<People>> result = sortByValueCount(ihm);
System.out.println("\nSorted Map :");
for (Map.Entry<Home, List<People>> entry:result.entrySet()) {
System.out.println(entry.getKey());
}
}
public static Map<Home, List<People>> sortByValueCount(final Map<Home,List<People>> homeListMap) {
return homeListMap.entrySet()
.stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
static class Home{
int size;
public Home(int size) {
this.size = size;
}
@Override
public String toString() {
return "Home{" +
"size=" + size +
'}';
}
}
static class People{
int peopleNumber;
public People(int peopleNumber) {
this.peopleNumber = peopleNumber;
}
}
为了更好地理解,我已将主页大小设置为等于其中元素的大小。
static <K,V extends Collection> Map<K,V> sortMap(Map<K,V> map){
return map.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.