forked from Rapter1990/JavaStreamAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFirstExample.java
More file actions
31 lines (21 loc) · 856 Bytes
/
FindFirstExample.java
File metadata and controls
31 lines (21 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package functionalexamples;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class FindFirstExample {
public static void main(String[] args) {
List<Integer> numbersList = List.of(5,1022,23,324,61,2);
System.out.println("List elements " + numbersList);
Optional<Integer> element = numbersList.stream()
.filter(n -> n%2 == 0)
.findFirst();
System.out.println("From list : " +element.orElseThrow());
Set<Integer> numbersSet = Set.of(5,1022,23,324,62,2);
System.out.println("Set elements " + numbersSet);
Optional<Integer> elementSet = numbersSet.stream()
.sorted()
.filter(n -> n%2 == 0)
.findFirst();
System.out.println("From set : " +elementSet.orElseThrow());
}
}