Skip to content

Commit 930ddfc

Browse files
committed
'update'
1 parent 4e9591b commit 930ddfc

File tree

6 files changed

+127
-37
lines changed

6 files changed

+127
-37
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lambdasinaction.chap10;
2+
3+
import java.util.Optional;
4+
import java.util.function.Consumer;
5+
6+
/**
7+
* @author vector
8+
* @date: 2018/10/10 0010 11:29
9+
*/
10+
public class Demo103 {
11+
public static void main(String[] args) {
12+
Person person = new Person();
13+
Optional<Car> car = person.getCar();
14+
System.out.println(car.orElse(new Car()));
15+
car.ifPresent(System.out::println);
16+
;
17+
18+
19+
// Optional<Car> optCar2 = Optional.of(car)
20+
21+
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package lambdasinaction.chap10;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
7+
/**
8+
* @author vector
9+
* @date: 2018/10/10 0010 14:00
10+
*/
11+
public class Demo104 {
12+
public static void main(String[] args) {
13+
Map<String, String> map = new HashMap<>();
14+
15+
map.put("a", "sadfas");
16+
map.put("b", "sadfas1");
17+
map.put("c", "sadfas2");
18+
map.put("d", "sadfas3");
19+
20+
String e = map.get("e");
21+
System.out.println(e);
22+
23+
Optional<String> e1 = Optional.ofNullable(map.get("e"));
24+
String s = Optional.ofNullable(map.get("e")).orElse("unknown key");
25+
System.out.println(s);
26+
27+
}
28+
29+
public static Optional<Integer> stringToInt(String s) {
30+
try {
31+
return Optional.of(Integer.parseInt(s));
32+
} catch (NumberFormatException e) {
33+
return Optional.empty();
34+
}
35+
}
36+
}
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
//package lambdasinaction.chap10;
2-
//
3-
//import java.util.*;
4-
//
5-
//import static java.util.Optional.of;
6-
//import static java.util.Optional.empty;
7-
//
8-
//public class OperationsWithOptional {
9-
//
10-
// public static void main(String... args) {
11-
// System.out.println(max(of(3), of(5)));
12-
// System.out.println(max(empty(), of(5)));
13-
//
14-
// Optional<Integer> opt1 = of(5);
1+
package lambdasinaction.chap10;
2+
3+
import java.util.*;
4+
5+
import static java.util.Optional.of;
6+
import static java.util.Optional.empty;
7+
8+
public class OperationsWithOptional {
9+
10+
public static void main(String... args) {
11+
System.out.println(max(of(3), of(5)));
12+
System.out.println(max(empty(), of(5)));
13+
14+
Optional<Integer> opt1 = of(5);
15+
1516
// Optional<Integer> opt2 = opt1.or(() -> of(4));
1617
//
1718
// System.out.println(
1819
// of(5).or(() -> of(4))
1920
// );
20-
// }
21-
//
22-
// public static final Optional<Integer> max(Optional<Integer> i, Optional<Integer> j) {
23-
// return i.flatMap(a -> j.map(b -> Math.max(a, b)));
24-
// }
25-
//}
21+
}
22+
23+
public static final Optional<Integer> max(Optional<Integer> i, Optional<Integer> j) {
24+
return i.flatMap(a -> j.map(b -> Math.max(a, b)));
25+
}
26+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
//package lambdasinaction.chap10;
2-
//
3-
//import java.util.*;
4-
//
5-
//import static java.util.stream.Collectors.toSet;
6-
//
7-
//public class OptionalMain {
8-
//
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-
//
1+
package lambdasinaction.chap10;
2+
3+
import java.util.*;
4+
5+
import static java.util.stream.Collectors.toSet;
6+
7+
public class OptionalMain {
8+
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+
1616
// public Set<String> getCarInsuranceNames(List<Person> persons) {
1717
// return persons.stream()
1818
// .map(Person::getCar)
@@ -21,4 +21,4 @@
2121
// .flatMap(Optional::stream)
2222
// .collect(toSet());
2323
// }
24-
//}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.stream.Stream;
4+
5+
/**
6+
* @author vector
7+
* @date: 2018/10/9 0009 11:21
8+
*/
9+
public class Demo71 {
10+
11+
public static long sequentialSum(int n) {
12+
return Stream.iterate(1L, i -> i + 1).limit(n).reduce(0L, (l1, l2) -> l1 + l2);
13+
}
14+
15+
public static long parallelSum(int n) {
16+
return Stream.iterate(1L, i -> i + 1).limit(n).parallel().reduce(0L, (l1, l2) -> l1 + l2);
17+
}
18+
19+
public static long iterativeSum(int n) {
20+
long result = 0;
21+
for (int i = 0; i < n; i++) {
22+
result += i;
23+
}
24+
return result;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
}
30+
}

src/main/java/lambdasinaction/chap8/Peek.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Peek {
1010

1111
public static void main(String[] args) {
1212

13-
List<Integer> result = Stream.of(2, 3, 4, 5)
13+
List<Integer> result = Stream.of(2, 3, 4, 5, 13, 23)
1414
.peek(x -> System.out.println("taking from stream: " + x)).map(x -> x + 17)
1515
.peek(x -> System.out.println("after map: " + x)).filter(x -> x % 2 == 0)
1616
.peek(x -> System.out.println("after filter: " + x)).limit(3)

0 commit comments

Comments
 (0)