Skip to content

Commit 12962fb

Browse files
authored
Merge pull request eugenp#8109 from martinvw/feature/BAEL-3392
[BAEL-3392] Debugging Java 8 Streams with IntelliJ
2 parents 641457a + e5132d9 commit 12962fb

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.streams.debug.entity;
2+
3+
public class Customer {
4+
private final String name;
5+
private final int age;
6+
7+
public Customer(String name, int age) {
8+
this.name = name;
9+
this.age = age;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public int getAge() {
17+
return age;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.streams.debug;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.stream.IntStream;
6+
7+
import org.junit.Test;
8+
9+
public class Example1 {
10+
@Test
11+
public void whenDebugging_thenInformationIsShown() {
12+
int[] listOutputSorted = IntStream.of(-3, 10, -4, 1, 3)
13+
.sorted()
14+
.toArray();
15+
16+
assertThat(listOutputSorted).isSorted();
17+
}
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.streams.debug;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Stream;
9+
10+
import org.junit.Test;
11+
12+
import com.baeldung.streams.debug.entity.Customer;
13+
14+
public class Example2 {
15+
@Test
16+
public void whenDebugging_thenInformationIsShown() {
17+
List<Optional<Customer>> customers = Arrays.asList(
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+
);
26+
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+
34+
assertThat(numberOf65PlusCustomers).isEqualTo(2);
35+
}
36+
}

0 commit comments

Comments
 (0)