gpt4 book ai didi

java - 如何在类对象属性的 HashMap 上使用可比性

转载 作者:行者123 更新时间:2023-11-30 03:46:28 26 4
gpt4 key购买 nike

我有一个足球表,我正在尝试按赢得的比赛进行排序。

我有一个类实现比较并查看赢得的属性。这似乎返回了正确的值(1、-1 或 0)。

class TeamStandings implements Comparable<TeamStandings>{
protected int matchesPlayed = 0;
protected int won = 0;
protected int draw = 0;
protected int loss = 0;
protected int goalsScored = 0;
protected int goalsConceded = 0;
protected int goalDifference = 0;
protected int points = 0;

@Override
public int compareTo(TeamStandings other){
// compareTo should return < 0 if this is supposed to be
// less than other, > 0 if this is supposed to be greater than
// other and 0 if they are supposed to be equal

//System.out.println(this.won + " " + other.won + " " + Integer.valueOf(this.won).compareTo(Integer.valueOf(other.won)));
return Integer.valueOf(this.won).compareTo(Integer.valueOf(other.won));

}
}

此排序函数应返回排序后的 HashMap 。 HashMap 以团队名称作为键,以 teamstands 类的实例作为值。

private static HashMap sortByValues(HashMap map) { 
List list = new LinkedList(map.entrySet());
// Defined Custom Comparator here
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {

System.out.println(((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()));
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});

// Here I am copying the sorted list in HashMap
// using LinkedHashMap to preserve the insertion order
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
//System.out.println(entry.getKey());
}
return sortedHashMap;
}

HashMap 排序表,采用团队名称字符串和包含获胜、失败比赛等的团队排名类对象。

由于某种原因,我无法对表格进行排序。它只是保持未排序的顺序。我相信这与 collection.sort 位置的 sortbyvalues 函数有关。

有什么想法可以使用我的类中的赢取值来对 HashMap 进行排序吗?

public void printTable(){

Map<String, TeamStandings> sortedTable = sortByValues(this.table);

System.out.println("Group: " + this.groupLetter);
System.out.println("Team"
+ "\t" + "MP"
+ "\t" + "W"
+ "\t" + "D"
+ "\t" + "L"
+ "\t" + "GS"
+ "\t" + "GC"
+ "\t" + "GD"
+ "\t" + "P");

//Iterator<Map.Entry<String, TeamStandings>> iterator = this.table.entrySet().iterator() ;
Iterator<Map.Entry<String, TeamStandings>> iterator = sortedTable.entrySet().iterator() ;

while(iterator.hasNext()){
Map.Entry<String, TeamStandings> team = iterator.next();
System.out.println(team.getKey()
+ "\t" + team.getValue().matchesPlayed
+ "\t" + team.getValue().won
+ "\t" + team.getValue().draw
+ "\t" + team.getValue().loss
+ "\t" + team.getValue().goalsScored
+ "\t" + team.getValue().goalsConceded
+ "\t" + team.getValue().goalDifference
+ "\t" + team.getValue().points);
}
}

谢谢

戴夫

编辑:

我认为这与这段代码有关,因为我打印了列表条目,但它们没有排序:

    List list = new LinkedList(map.entrySet());
// Defined Custom Comparator here
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {

System.out.println(((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()));
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});

我将代码改回了最初发布的代码,这与 LinkedList 或 LinkedHashMap 无关,代码实际上一直有效,只是按升序排序。这对于足球联赛表来说显然是不正确的。

编辑:

修复方法是添加此处显示的 * -1:

return Integer.valueOf(this.won).compareTo(Integer.valueOf(other.won)) * -1;

感谢@Justin 和@falsarella。

最佳答案

显然,正如 @Justin 评论的那样,输出排序,但按升序顺序排列:

0, 1, 2, 3

如果您希望按后代顺序排列,只需更改从compareTo方法返回的整数符号即可:

return Integer.valueOf(this.won).compareTo(Integer.valueOf(other.won)) * -1; 

关于java - 如何在类对象属性的 HashMap 上使用可比性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com