0

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:

  • add Gamer in an ArrayList if not already present
  • if present, add ++ to a parameter named LeaderPoints
  • sort by point

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()));
}
1
  • can you show the Gamer class Commented Feb 26, 2019 at 12:59

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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());

Steps:

  1. extract winner
  2. group winners by name and sum their scores. As result you will get Map
  3. get entrySet of this Map
  4. open stream on this entySet
  5. sort by scores in DESC direction
  6. map entry to Gamer (create new gamer instance for each entry)
  7. collect to list

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());

1 Comment

you are welcome :) Streams help us not to write redundant code ;)
0

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());
        }
    }
}

1 Comment

Look good ! but i got a error on indexOf ... i don't know why ?
0

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);
        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.