Skip to content

Instantly share code, notes, and snippets.

@kunals201
Created March 29, 2018 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunals201/13ab0ace0de381ffaa56ad2b0558be85 to your computer and use it in GitHub Desktop.
Save kunals201/13ab0ace0de381ffaa56ad2b0558be85 to your computer and use it in GitHub Desktop.
Imperative style example of ifPresent
public class IfPresentOrElseExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90);
Optional<Integer> first = numbers.stream().filter(a -> a > 500).findFirst();
if (first.isPresent()) {
System.out.println(first.get());
} else {
System.out.println("Value not found");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment