Reputation: 7
I'm facing a problem that I don't arrive to resolve:
I read an ArrayList
from the phone:
ArrayList<Game> gameResult = writeRead.getArrayList(this);
I put the object gamer
from each game
in a new ArrayList
:
ArrayList<Gamer> gamerList = new ArrayList<>();
for (int i = 0; i < gameResult.size(); i++) {
gamerList.add(gameResult.get(i).getWinnerGamer());
}
now, what I would like is to:
Gamer
in an ArrayList
if not already presentLeaderPoints
It's about a leaderboard activity with a custom ArrayList
, but I don't arrive to do it.
All my code:
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()));
}
Upvotes: 0
Views: 87
Reputation: 7
Finally i did it like this :
and it work perfectly Thanks !
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);
}
}
Upvotes: 0
Reputation: 785
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());
Steps:
My answer based on assumption that you have such structure :
class Game {
private Gamer winnerGamer;
// getter & setter
}
class Gamer {
private String name;
private Integer leaderboardScore;
// getters & setters && AllArgsContructor
}
BTW: if you override equals and hash code in class Gamer you can avoid Gamer recreation with this:
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());
Upvotes: 0
Reputation:
you can use Set
Instead of ArrayList
for unique value.
override HashCode and Equal Method.
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());
}
}
}
Upvotes: 0
Reputation: 5636
Override equals and hashCode methods of your Gamer object class.
Then before adding object to ArrayList, check Arraylist.contains()
, if yes then you can either call ArrayList.indexOf()
method to get that object instance in ArrayList
, increment the value of LeaderPoints and call ArrayList.set()
to set back the instance at the same position.
Upvotes: 1