Created
March 29, 2018 07:33
-
-
Save kunals201/13ab0ace0de381ffaa56ad2b0558be85 to your computer and use it in GitHub Desktop.
Imperative style example of ifPresent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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