|
1 | 1 | package com.baeldung.streams.debug;
|
2 | 2 |
|
3 |
| -import com.baeldung.streams.debug.entity.Customer; |
4 |
| -import org.junit.Test; |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
5 | 4 |
|
6 | 5 | import java.util.Arrays;
|
7 | 6 | import java.util.List;
|
8 | 7 | import java.util.Optional;
|
9 | 8 | import java.util.stream.Stream;
|
10 | 9 |
|
11 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.baeldung.streams.debug.entity.Customer; |
12 | 13 |
|
13 | 14 | public class Example2 {
|
14 | 15 | @Test
|
15 | 16 | public void whenDebugging_thenInformationIsShown() {
|
16 | 17 | List<Optional<Customer>> customers = Arrays.asList(
|
17 |
| - Optional.of(new Customer("John P.", 15)), |
18 |
| - Optional.of(new Customer("Sarah M.", 78)), |
19 |
| - Optional.empty(), |
20 |
| - Optional.of(new Customer("Mary T.", 20)), |
21 |
| - Optional.empty(), |
22 |
| - Optional.of(new Customer("Florian G.", 89)), |
23 |
| - Optional.empty()); |
| 18 | + Optional.of(new Customer("John P.", 15)), |
| 19 | + Optional.of(new Customer("Sarah M.", 78)), |
| 20 | + Optional.empty(), |
| 21 | + Optional.of(new Customer("Mary T.", 20)), |
| 22 | + Optional.empty(), |
| 23 | + Optional.of(new Customer("Florian G.", 89)), |
| 24 | + Optional.empty() |
| 25 | + ); |
24 | 26 |
|
25 |
| - long numberOf65PlusCustomers = customers |
26 |
| - .stream() |
27 |
| - .flatMap(c -> c |
28 |
| - .map(Stream::of) |
29 |
| - .orElseGet(Stream::empty)) |
30 |
| - .mapToInt(Customer::getAge) |
31 |
| - .filter(c -> c > 65) |
32 |
| - .count(); |
| 27 | + long numberOf65PlusCustomers = customers.stream() |
| 28 | + .flatMap(c -> c.map(Stream::of) |
| 29 | + .orElseGet(Stream::empty)) |
| 30 | + .mapToInt(Customer::getAge) |
| 31 | + .filter(c -> c > 65) |
| 32 | + .count(); |
33 | 33 |
|
34 | 34 | assertThat(numberOf65PlusCustomers).isEqualTo(2);
|
35 | 35 | }
|
|
0 commit comments