我试图模拟多个玩家可以提交游戏分数的游戏板。按排序顺序存储条目并检索条目周围的条目
POJO即, Entry.java代表排行榜中的一个条目。 请注意重写equals()方法。
位置在排行榜的位置,1为具有 最高的用户分值
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 + "]";
}}
该游戏键盘类有两个方法:
- submitScore(字符串uid,int score)每个玩家都会调用这个方法将他的分数提交给游戏棋盘。有每位玩家/用户只有一项,因此,如果如果用户是在排行榜玩家调用此方法多次,他的最新得分存储
- getLeaderBoard(字符串UID)
用户后立即最大的两个条目(即是正上方 用户在领先的用户),用户自己的条目,最大的两个条目,返回具有 更大的成绩比用户在排行榜
e.g:
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<EntryMapOption, String> 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<EntryMapOption> getLeaderBoard(String uid) {
System.out.println("---------Current leader board---------");
leaderBoardEntryUserMap.keySet().forEach(System.out::println);
List<EntryMapOption> 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<EntryMapOption> entriesOptionTwo = new ArrayList<>();
entriesOptionTwo.add(leaderBoardEntryUserMap.higherKey(userEntry));
entriesOptionTwo.add(userEntry);
entriesOptionTwo.add(leaderBoardEntryUserMap.lowerKey(userEntry));
return entriesOptionTwo;
}
}
与上面的代码的问题:
- 当(理想地,submitScore期间()),以及如何应在 '位置' 来计算。虽然它用于键,但我想知道Map.compute()是否能以任何方式提供帮助!
- 检查下面的代码//不完整和容易出错的注释 虽然'higherKey()'和'lowerKey()'来得方便,但我不确定如何使用它们来选择固定数量的条目特别是进入
*****编辑-1 ****** @霍尔格的修复解决了以下问题
- 我无法弄清楚如何解决平等之间的矛盾( )和compare()。这造成缺少项
'Comparator.comparingInt(EntryMapOption :: getScore).reversed()。thenComparingInt(EntryMapOption ::的getuid)'... – Holger
@Holger解决了第一个问题:) 更新原始比较代码。 –
您不得修改影响其查找逻辑的关键字的属性,例如“HashMap”时的哈希码或“TreeMap”情况下的排序属性。如果它具有自然顺序,则在两种情况下都不能改变。您必须首先移除并在修改后重新添加,或者执行批量操作以创建新的“Map”。关于'higherKey'和'lowerKey',目前还不清楚你的问题在哪里。 – Holger