java顺序存储_java-按排序顺序存储条目并在条目周围检索...

我正在尝试模拟一个游戏板,多个玩家可以提交他们的游戏分数.

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()之间的不一致.这导致缺少条目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值