I have an arraylist, aliens
, of the following objects:
public class Alien{
public int[] pos;
public int speed;
public Alien(int[] pos, int speed) {
this.pos = pos;
this.speed = speed;
}
@Override
public String toString() {
return "(" + this.speed + ")[" + this.pos[0] + "," + this.pos[1] + "]";
}
}
and I've gotten this far with what I'm looking for, assuming I'm going in the right direction:
aliens.stream()
.filter(x -> x.pos[1] == SpaceInvaders.ship)
.collect(Collectors.groupingBy(x -> x.pos[0]));
where SpaceInvaders.ship
is an int representing the column that I'm looking to intersect on. If you print this out, it shows {0=[(2)[0,4], (-3)[0,4]], 1=[(-7)[1,4]]}
, indicating that they are correctly grouped by the first element of their position. As you can see, the positions aren't necessarily unique, so I need to do some processing on a group to determine other criterion, but I need to only process the group with the highest value key. If I use maxBy
the way that most of the online documentation describes, I get the max for each group, but I need only the max of the groups by the key representing the row the alien is on.
I'm stuck at this point. How does one do a maxBy
on the keys of the return value of a collect(groupingBy())
?