3

I'm trying to return an object from an arraylist I have, which contains different opponents for my text-based fighting game. The opponents in the arraylist have different "deadlylevels", to seperate one easy opponent from a tough one.

To explain it differently; Let's say I have three opponents in my arraylist, and they have the deadlylevels of 30, 40 and 50, respectively. I want to return the most deadliest of them, so that would be the opponent who has a deadlylevel of 50.

I have already given them different deadlylevels, but I'm not sure how to return the opponent that has the highest deadlylevel.

3
  • Sort the List with a Comparator that uses the "deadlylevel" as criterion and get the first (or last) element. Commented Apr 16, 2015 at 23:05
  • @MickMnemonic Thanks, I'll see if I can get it to work! Commented Apr 16, 2015 at 23:37
  • Paul's answer will do exactly what I suggested, but with less code, so you should probably choose that solution. Commented Apr 16, 2015 at 23:43

4 Answers 4

6

If you wanted the maximum level, that would just be max(). But you want the max opponent, by level. In Eclipse Collections, we call that pattern maxBy(). If you can replace your ArrayList with MutableList then it becomes:

MutableList<Opponent> opponents = ...;
Opponent opponent = opponents.maxBy(Opponent::getDeadlyLevel);

If you can't or won't convert the ArrayList, you can still use static utility on Iterate.

Opponent opponent = Iterate.maxBy(opponents, Opponent::getDeadlyLevel);

You could use Collections.max() along with Comparator.comparing() to build the Comparator.

Opponent opponent =
    Collections.max(opponents, Comparator.comparing(Opponent::getDeadlyLevel));

Or you could use Java 8 Streams.

Optional<Opponent> optionalOpponent = 
    opponents.stream().max(Comparator.comparing(Opponent::getDeadlyLevel));
Opponent opponent = optionalOpponent.get(); // If you're sure there's at least one

Note: I am a committer for Eclipse Collections.

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

Comments

4

Create a Comparator that compares the levels of the oponents and use Collections.max(oponentList , theComparator).

Comments

3

Use Stream#max() providing a function for the relevant attribute:

List<Fighter> list; // given a list of Fighters
Fighter deadliest = list.stream()
    .max((a,b) -> a.getDeadlyLevel() - b.getDeadlyLevel())
    .get();

2 Comments

The example doesn't compile.
@CraigP.Motlin oops - thumbing code on train (coded a Function not a BiFunction). Fixed now - thx.
1

Use Comparator.comparing

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class DeathMatch {
    public static void main(String[] arg){
        List<Opponent> list = Arrays.asList(new Opponent("ZZ",10),new Opponent("WW",50),new Opponent("MM", 30));
        Opponent deadliest = list.stream().max(Comparator.comparing(p->p.getLevel())).get();
        System.out.println(deadliest.getName()+","+ deadliest.getLevel());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.