-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickAnElement.java
38 lines (31 loc) · 1.58 KB
/
PickAnElement.java
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
32
33
34
35
36
37
38
/*
* Requirement: Given a list find the first item that starts with the given letter.
* Print it if exist, else print not found
*/
import java.util.*;
public class PickAnElement {
public static void main(String[] args) {
List<String> friends = List.of("Abc", "Acd", "Bbbc", "Bcd", "Scd", "Sdd");
String startingLetter = "S";
Optional<String> foundName = friends.stream().filter(name -> name.startsWith(startingLetter)).findFirst();
System.out.println(
String.format("A name starting with letter %s: %s", startingLetter, foundName.orElse("No name found")));
// Optional variable can also process a lambda expression if only the value is
// present
foundName.ifPresent(name -> System.out.println("Hello " + name));
/*
* Optional: It was introduced in version 1.8 to act as state-appointed null
* deodorizer (Bravo, Venkat Subramanian)
* Methods Present:
* 1. isPresnt -> returns true if value is present else false
* 2. get -> return the value else throw NoSuchElementException
* 3. orElse(Object other) -> returns the value if present, or else return the
* default value
* 4. ifPresent(Consumer) -> consume the value if present otherwise do nothing
* 5. orElseGet(Supplier) -> returns the value if present else invoke the
* supplier and return the result
* 6. orElseThrow() -> Return the contained value, if present, otherwise throw
* an exception to be created by the provided supplier.
*/
}
}