In method 3 you could print it only if it exists by this way : // Method 3: find the element that matches the condition User bobby = users.stream() .filter(user -> userToFind.equals(user.name)) .findFirst().ifPresent(b -> System.out.println("Bobby : " +b));
In method 3 you could print it only if it exists by this way :
// Method 3: find the element that matches the condition
User bobby = users.stream()
.filter(user -> userToFind.equals(user.name))
.findFirst().ifPresent(b -> System.out.println("Bobby : " +b));