|
1 | 1 | package lambdasinaction.chap10; |
2 | 2 |
|
3 | 3 | import java.util.*; |
4 | | - |
5 | | -import static java.util.stream.Collectors.toSet; |
| 4 | +import java.util.function.Predicate; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.IntStream; |
| 7 | +import java.util.stream.Stream; |
6 | 8 |
|
7 | 9 | public class OptionalMain { |
8 | 10 |
|
9 | | - public String getCarInsuranceName(Optional<Person> person) { |
10 | | - return person.flatMap(Person::getCar) |
11 | | - .flatMap(Car::getInsurance) |
12 | | - .map(Insurance::getName) |
13 | | - .orElse("Unknown"); |
14 | | - } |
15 | 11 |
|
16 | | -// public Set<String> getCarInsuranceNames(List<Person> persons) { |
| 12 | + // public Set<String> getCarInsuranceNames(List<Person> persons) { |
17 | 13 | // return persons.stream() |
18 | 14 | // .map(Person::getCar) |
19 | 15 | // .map(optCar -> optCar.flatMap(Car::getInsurance)) |
20 | 16 | // .map(optInsurance -> optInsurance.map(Insurance::getName)) |
21 | 17 | // .flatMap(Optional::stream) |
22 | 18 | // .collect(toSet()); |
23 | 19 | // } |
| 20 | + public static void main(String[] args) { |
| 21 | + Stream<String> stream = Stream.of("lamurudu", "Okanbi", "Oduduwa"); |
| 22 | + Optional<String> f = stream.filter(name -> name.startsWith("L")).findFirst(); |
| 23 | + String lambda = f.orElse("Lambda"); |
| 24 | + // String lambda = f.orElseGet(() -> "Lambda"); |
| 25 | + f.ifPresent(s -> { |
| 26 | + s = s.toUpperCase(); |
| 27 | + System.out.println(s); |
| 28 | + }); |
| 29 | + System.out.println(lambda); |
| 30 | + |
| 31 | + |
| 32 | + Optional.ofNullable("Jackson").filter(s -> s.equals("Jackson")).ifPresent(s -> System.out.println(s + "~~~")); |
| 33 | + |
| 34 | + Stream.of("lamurudu", "Okanbi", "Oduduwa").filter(s -> s.contains("c")).findFirst().ifPresent(s -> System.out.println(123)); |
| 35 | + |
| 36 | + Set<String> words = new HashSet<>(Arrays.asList("this", "is", "a", "stream", "of", "strings")); |
| 37 | + Optional<String> firstString = words.stream() |
| 38 | + .findFirst(); |
| 39 | + System.out.println(firstString); |
| 40 | + |
| 41 | + List<String> equal = IntStream.range(0, 100) |
| 42 | + .mapToObj(i -> new String("test")) // don't do this in normal code |
| 43 | + .collect(Collectors.toList()); |
| 44 | + Map<String, Integer> map = IntStream.range(0, equal.size()) |
| 45 | + .collect(IdentityHashMap::new, (m, i) -> m.put(equal.get(i), i), Map::putAll); |
| 46 | + |
| 47 | + equal.parallelStream().distinct().map(map::get) |
| 48 | + .findFirst().ifPresent(System.out::println); |
| 49 | + |
| 50 | + |
| 51 | + Predicate<String> startsWithJ = (n) -> n.startsWith("J"); |
| 52 | + Predicate<String> fourLetterLong = (n) -> n.length() == 4; |
| 53 | + Predicate<String> containsBig = (n) -> n.contains("A"); |
| 54 | + Stream.of("lamurudu", "Jack", "Oduduwa", "Jbulsu Abas") |
| 55 | + .filter(startsWithJ.and(fourLetterLong.or(containsBig))) |
| 56 | + .forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n)); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public String getCarInsuranceName(Optional<Person> person) { |
| 61 | + return person.flatMap(Person::getCar).flatMap(Car::getInsurance).map(Insurance::getName).orElse("Unknown"); |
| 62 | + } |
24 | 63 | } |
0 commit comments