我正在尝试模拟一个游戏板,多个玩家可以提交他们的游戏分数.
POJO即Entry.java表示排行榜中的一个条目.请注意,重写的equals()方法.
Position is the position in the leaderboard, 1 being the user with the
highest score
public class EntryTreeMapOption {
private String uid;
private int score;
private int position;
public EntryTreeMapOption(String uid, int score) {
this.uid = uid;
this.score = score;
}
public EntryTreeMapOption() {
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uid == null) ? 0 : uid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EntryTreeMapOption other = (EntryTreeMapOption) obj;
if (uid == null) {
if (other.uid != null)
return false;
} else if (!uid.equals(other.uid))
return false;
return true;
}
@Override
public String toString() {
return "Entry [uid=" + uid + ", score=" + score + ", position=" + position + "]";
}}
GameBoard类具有两种方法:
> SubmitScore(String uid,int score)每个玩家都调用此方法将其分数提交给游戏板.每个玩家/用户只有一个条目,因此,如果玩家多次调用此方法,则会存储他的最新分数
> getLeaderBoard(字符串uid)
If the user is in the leaderboard, returns max two entries that have
larger score than the user (the users that are immediately above the
user in the leaderboard), the user’s own entry and max two entries
that are immediately after the user in the leaderboard
例如:
The leader board is :
Entry [uid=user1, score=14, position=1]
Entry [uid=user2, score=8, position=2]
Entry [uid=user3, score=7, position=3]
Entry [uid=user4, score=7, position=3]
Entry [uid=user5, score=4, position=4]
Entry [uid=user6, score=3, position=5]
Entry [uid=user7, score=3, position=5]
Entry [uid=user8, score=1, position=6]
For user5, entries returned should be :
Entry [uid=user3, score=7, position=3]
Entry [uid=user4, score=7, position=3]
Entry [uid=user5, score=4, position=4]
Entry [uid=user6, score=3, position=5]
Entry [uid=user7, score=3, position=5]
For user4, entries returned should be :
Entry [uid=user1, score=14, position=1]
Entry [uid=user2, score=8, position=2]
Entry [uid=user4, score=7, position=3]
Entry [uid=user5, score=4, position=4]
Entry [uid=user6, score=3, position=5]
For user6, entries returned should be :
Entry [uid=user4, score=7, position=3]
Entry [uid=user5, score=4, position=4]
Entry [uid=user6, score=3, position=5]
Entry [uid=user8, score=1, position=6]
For user7, entries returned should be :
Entry [uid=user4, score=7, position=3]
Entry [uid=user5, score=4, position=4]
Entry [uid=user7, score=3, position=5]
Entry [uid=user8, score=1, position=6]
我最初的方法是使用TreeMap,替代方法是here.
public class GameDefault2 {
private TreeMap leaderBoardEntryUserMap;
{
leaderBoardEntryUserMap = new TreeMap<>(Comparator.comparingInt(EntryTreeMapOption::getScore).reversed()
.thenComparing(EntryTreeMapOption::getUid));
}
@Override
public void submitScore(String uid, int score) {
EntryMapOption newEntry = new EntryMapOption(uid, score);
leaderBoardEntryUserMap.put(newEntry, uid);
}
@Override
public List getLeaderBoard(String uid) {
System.out.println("---------Current leader board---------");
leaderBoardEntryUserMap.keySet().forEach(System.out::println);
List userEntryList = leaderBoardEntryUserMap.entrySet().stream()
.filter(entry -> uid.equalsIgnoreCase(entry.getKey().getUid())).map(Map.Entry::getKey)
.collect(Collectors.toList());
if (userEntryList == null || userEntryList.isEmpty())
return Collections.emptyList();
// Incomplete and error prone
EntryMapOption userEntry = userEntryList.get(0);
List entriesOptionTwo = new ArrayList<>();
entriesOptionTwo.add(leaderBoardEntryUserMap.higherKey(userEntry));
entriesOptionTwo.add(userEntry);
entriesOptionTwo.add(leaderBoardEntryUserMap.lowerKey(userEntry));
return entriesOptionTwo;
}
}
上面代码的问题:
>何时(理想情况下,在SubmitScore()期间)以及如何计算“位置”.尽管它用于键,但我想知道Map.compute()是否可以以任何方式提供帮助!
>检查“ //不完整且容易出错”注释下的代码
虽然’higherKey()’和’lowerKey()’很方便,但我不确定如何使用它们来选择特定条目上下的固定数量的条目
*****编辑-1 ******
@Holger的修复解决了以下问题
>我无法弄清楚如何解决equals()和compare()之间的不一致.这导致缺少条目