Skip to content

Commit fffd549

Browse files
committed
'merge'
2 parents 930ddfc + b21c2b1 commit fffd549

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed
Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
11
package lambdasinaction.chap10;
22

33
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;
68

79
public class OptionalMain {
810

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-
}
1511

16-
// public Set<String> getCarInsuranceNames(List<Person> persons) {
12+
// public Set<String> getCarInsuranceNames(List<Person> persons) {
1713
// return persons.stream()
1814
// .map(Person::getCar)
1915
// .map(optCar -> optCar.flatMap(Car::getInsurance))
2016
// .map(optInsurance -> optInsurance.map(Insurance::getName))
2117
// .flatMap(Optional::stream)
2218
// .collect(toSet());
2319
// }
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+
}
2463
}

0 commit comments

Comments
 (0)