-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptional.java
More file actions
30 lines (22 loc) · 860 Bytes
/
Copy pathOptional.java
File metadata and controls
30 lines (22 loc) · 860 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
import java.util.*;
public class Demo {
public static void main(String [] args){
List<String> names = Arrays.asList("DJ","Dhanunjay","DJAY","DJAI");
Optional<String> name = names.stream()
.filter(str -> str.contains("D"))
.findFirst();
System.out.println(name.orElse("Not Found"));
}
}
// or you can use this.
import java.util.*;
public class Demo {
public static void main(String [] args){
List<String> names = Arrays.asList("DJ","Dhanunjay","DJAY","DJAI");
String name = names.stream()
.filter(str -> str.contains("D"))
.findFirst()
.orElse("Not Found");
System.out.println(name);
}
}