public Player findPlayerByUsername(final String username) {
return players.stream().filter(p -> p.getUsername().equalsIgnoreCase(username)).findFirst().orElse(null);
}
The method findFirst()
returns Optional<Player>
.
If the option has a player object, optional.get()
will return that object. If the object does not exist and you need an alternative, specify this option in
.orElse(new Player()); or .orElse(null)
See Supplementary Documentation and Supplementary Tutorial for more details.
source
share