user3826166
user3826166

Reputation: 39

Maximum number arraylist

Hello i have to get maximum number from arraylist in Java . ia m getting error T- is not within bound implement java . lang . comparable is there any other way to sort arralist . Here is my code sample .

private void CreateHighestScorePlayer(LinearLayout layoutForHighScore) {
    HighScoreManager highScoreManager = HighScoreManager.getInstance(getApplicationContext());
    ArrayList<Score> scores = highScoreManager.getScore();
    Collections.sort(scores);
    scores.get(scores.size() -1);

}

Upvotes: 0

Views: 173

Answers (4)

wassgren
wassgren

Reputation: 19241

If you simply wish to find the highest score you can use a simple Java 8 construct. If your Score class looks something like the following:

public static class Score {
    private final int score;

    Score(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }
}

Then, you can stream your List<Score> like this:

List<Score> scores =
        Arrays.asList(new Score(100), new Score(200), new Score(50));

final Optional<Score> max =
        scores.stream().max((score1, score2) -> Integer.compare(score1.getScore(), score2.getScore()));

if (max.isPresent()) {
    Score score = max.get();

    // Do stuff
} else {
    // Handle when there are no scores

}

This also uses a Comparator as described in some of the other answers. The Comparator is constructed as a lamdba like this:

(score1, score2) -> Integer.compare(score1.getScore(), score2.getScore())

Upvotes: 0

kraskevich
kraskevich

Reputation: 18586

If you want to get the maximum element, you should use Collections.max method. It has a version that takes a custom comparator as an argument.

Something like this:

Score result = Collections.max(scores, new Comparator<Score>() {
    @Override
    public int compare(Score score1, Score score2) {
        // Compare them here
    }
});

Upvotes: 1

Vidya
Vidya

Reputation: 30320

You need to have your Score object implement Comparable and then call Collections.max on your ArrayList. Or you can call the overloaded version of max with a Comparator. Either way, your code needs to know what makes one Score object bigger, smaller, or equal to another.

We actually created a video tutorial with sample code on Comparable and Comparator you can find here. The key thing is understanding the difference between the two in deciding which to use.

Upvotes: 2

Jack
Jack

Reputation: 133679

Collections.sort is a generic method with the following signature:

public static <T extends Comparable<? super T>> void sort(List<T> list)

This means that you must pass as an argument a List<T> where T extends Comparable<? super T>.

So you have two solutions, you can turn your Score class into

class Score implements Comparable<Score> {

  public int compareTo(Score other) {
    ...
  }
}

Or you can use Collection.sort(List<T>, Comparator<? super T> c) by passing a custom Comparator for your score class.

The first solution is better assuming you have control over Score class since it will give a score the feature to be naturally ordered with other scores.

Upvotes: 3

Related Questions