I'm new to Java 8 lambdas and streams. For practice, i'm trying to "translate" some methods from a school project into lambdas/streams when possible. Here it's the old method.
As you can see, i need to give to discard method a int, which is the number of the first playable card into the hand of the player.
public Card autoPlay(Card flipped) {
for (int i = 0; i < super.getHand().size(); i++) {
if (super.getHand().get(i).isCompatible(flipped)) { //check if card from hand is compatible with flipped card (same number or color)
return (this.discard(i)); //remove from hand and returns it
}
}
return null; //if any compatible card found
}
So far, the new version of my method is:
public Card newAutoPlay(Card flipped){
return super.getHand().stream().filter(card -> card.isCompatible(flipped)).findFirst().get();
}
I found out how to get the first compatible card, but not how to get it's index... I read about IntStreams (and using the size of the list as a range), but i don't understand how to use it in this particular case.
Is it possible to achieve what i'm trying to do, or it's not a good idea to do this?
EDIT: After exploring the question, using streams in this case brings too much complexity and not enough readability for solving this simple problem. So i've choose to a for loop. Also some other changes, but the core idea remains the same. Thanks all for helping.
@Override
public String play(Uno unoGame) {
int i = 0;
boolean found = false;
for (i = 0; i < super.getHand().size() && !found; i++) {
found = super.getHand().get(i).isCompatible(unoGame.getFlippedCard());
}
return found ? "play " + i : "draw";
}