从集合中获取记录并计算每个玩家 ID 的记录数
Take records from set and count how many records for each player id
我正在我的游戏中制作排行榜,并且我有分配了特定玩家 ID 的图块。
我需要获取以下数据(这是一组包含我的 'Tile' 条记录):
[Tile[id=eeebeaa4e03c8910e5c17925439f1fa1abb,position=Vec[x=-11.0, y=6.0, z=73.0], player_id=1oyr8l], Tile[id=3c10969e83a61f44139d5dbeb8b41c9e,position=Vec[x=-12.0, y=6.0, z=73.0], player_id=1oyr8l], Tile[id=f51fd6f3eb407305a49bde1cb2cf44fe,position=Vec[x=-12.0, y=6.0, z=74.0], player_id=qX9Bh7]]
我想将其格式化为:
1oyr8l: 2
qX9Bh7: 1
或代码形式:
public record Tile(String id, Vec position, String owner) {
// irrelevant code here
}
private final Set<Tile> userTiles = getTiles() // dummy func but you get the idea
我想采用 userTiles
,这是一个 Set<Tile>
,并将其转换为我在上面以文本格式显示的排序和计数格式,但采用 HashMap
的形式,如:
HashMap<String,Integer> leaderboard = new HashMap<String,Integer>();
遍历您的 Tiles。 for(Tile t : getTiles())
提取玩家ID。 String playerId = t.player_id()
检查玩家ID是否存在于HashMap中。 if leaderboard.containsKey(playerId)
如果是,请增加该键的值。
如果没有,添加值为1的地图。
我正在我的游戏中制作排行榜,并且我有分配了特定玩家 ID 的图块。
我需要获取以下数据(这是一组包含我的 'Tile' 条记录):
[Tile[id=eeebeaa4e03c8910e5c17925439f1fa1abb,position=Vec[x=-11.0, y=6.0, z=73.0], player_id=1oyr8l], Tile[id=3c10969e83a61f44139d5dbeb8b41c9e,position=Vec[x=-12.0, y=6.0, z=73.0], player_id=1oyr8l], Tile[id=f51fd6f3eb407305a49bde1cb2cf44fe,position=Vec[x=-12.0, y=6.0, z=74.0], player_id=qX9Bh7]]
我想将其格式化为:
1oyr8l: 2
qX9Bh7: 1
或代码形式:
public record Tile(String id, Vec position, String owner) {
// irrelevant code here
}
private final Set<Tile> userTiles = getTiles() // dummy func but you get the idea
我想采用 userTiles
,这是一个 Set<Tile>
,并将其转换为我在上面以文本格式显示的排序和计数格式,但采用 HashMap
的形式,如:
HashMap<String,Integer> leaderboard = new HashMap<String,Integer>();
遍历您的 Tiles。 for(Tile t : getTiles())
提取玩家ID。 String playerId = t.player_id()
检查玩家ID是否存在于HashMap中。 if leaderboard.containsKey(playerId)
如果是,请增加该键的值。
如果没有,添加值为1的地图。