Java Lambda style

  • A+
Category:Languages

So, this doesn't work, since seatsAvailable is final. How can what I'm trying to accomplish be done using more of a lambda-style-from-the-ground-up way?

final boolean seatsAvailable = false; theatreSeats.forEach(seat -> {     if (!seatsAvailable) seatsAvailable = seat.isEmpty(); }); 

 


It looks like you want seatsAvailable to be true if there is at least one empty seat. Therefore, this should do the trick for you:

final boolean seatsAvailable = theatreSeats.stream().anyMatch(Seat::isEmpty); 

(Note: I am assuming that your class is named Seat.)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: