我面临着一个无法解决的问题:
我从手机上读了一个ArrayList
:
ArrayList<Game> gameResult = writeRead.getArrayList(this);
我将每个gamer
中的对象game
放入新的ArrayList
中:
ArrayList<Gamer> gamerList = new ArrayList<>();
for (int i = 0; i < gameResult.size(); i++) {
gamerList.add(gameResult.get(i).getWinnerGamer());
}
现在,我想做的是
Gamer
中添加ArrayList
(如果尚不存在)LeaderPoints
的参数中这与带有自定义ArrayList
的排行榜活动有关,但我还没有这样做。
我所有的代码:
private void setGamerList() {
ArrayList<Game> gameResult = writeRead.getArrayList(this);
ArrayList<Gamer> gamerList = new ArrayList<>();
ArrayList<Gamer> gamerNoDuplicate = new ArrayList<>();
if (gameResult != null) {
for (int i = 0; i < gameResult.size(); i++) {
gamerList.add(gameResult.get(i).getWinnerGamer());
}
}
Log.i(TAG, "--------------------------------");
Collections.sort(mGamerList, (o1, o2) ->
o2.getLeaderboardScore().compareTo(o1.getLeaderboardScore()));
}
答案 0 :(得分:1)
覆盖Gamer对象类的等于和hashCode方法。
然后在将对象添加到ArrayList之前,检查Arraylist.contains()
,如果是,则可以调用ArrayList.indexOf()
方法以在ArrayList
中获得该对象实例,增加LeaderPoints的值并调用{ {1}}将实例回退到同一位置。
答案 1 :(得分:0)
您可以使用Set
代替ArrayList
来获得唯一值。
覆盖HashCode和等于方法。
Set<Gamer> gamerNoDuplicate=new HashSet();
if (gameResult != null) {
for (int i = 0; i < gameResult.size(); i++) {
if(gamerNoDuplicate.contains(gameResult.get(i).getWinnerGamer()){
Gamer alreadyExist = gamerNoDuplicate.indexOf(gameResult.get(i).getWinnerGamer());
alreadyExist.setLeaderPoints(alreadyExist.getLeaderPoints()+ gameResult.get(i).getWinnerGamer().getLeaderPoints());
}else{
gamerNoDuplicate.add(gameResult.get(i).getWinnerGamer());
}
}
}
答案 2 :(得分:0)
writeRead.getArrayList(this).stream()
.map(Game::getWinnerGamer)
.collect(Collectors.groupingBy(Gamer::getName, Collectors.summingInt(Gamer::getLeaderboardScore)))
.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry<String, Integer>::getValue).reversed())
.map(entry -> new Gamer(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
步骤:
我的回答是基于您具有这样的结构的假设:
class Game {
private Gamer winnerGamer;
// getter & setter
}
class Gamer {
private String name;
private Integer leaderboardScore;
// getters & setters && AllArgsContructor
}
顺便说一句:如果您在类Gamer中重写了equals和哈希码,则可以避免Gamer对此重新玩耍:
writeRead.getArrayList(this).stream()
.map(Game::getWinnerGamer)
.collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(Gamer::getLeaderboardScore)))
.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry<Gamer, Integer>::getValue).reversed())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
答案 3 :(得分:0)
最后我做到了:
,它运行完美 谢谢!
if (gameResult != null) {
for (int i = 0; i < gameResult.size(); i++) {
gamerList.add(gameResult.get(i).getWinnerGamer());
}
/* --------- */
for (int i = 0; i < gamerList.size(); i++) {
Gamer gamer = gamerList.get(i);
gamer.setLeaderboardScore(1);
boolean isInArray = false;
if (!gamerNoDuplicate.isEmpty()) {
for (int j = 0; j < gamerNoDuplicate.size(); j++) {
if (gamer.getName().equals(gamerNoDuplicate.get(j).getName())) {
gamerNoDuplicate.get(j).addLeaderscorePoint();
isInArray = true;
}
}
if (!isInArray) {
gamerNoDuplicate.add(gamer);
}
} else gamerNoDuplicate.add(gamer);
}
}