Skip to content

Commit b264937

Browse files
committed
[BAEL-3392] Add code samples for BAEL-3392 Debugging Java 8 Streams with IntelliJ
1 parent 18c6a1a commit b264937

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 org.junit.Test;
4+
5+
import java.util.stream.IntStream;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
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 com.baeldung.streams.debug.entity.Customer;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Optional;
9+
import java.util.stream.Stream;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class Example2 {
14+
@Test
15+
public void whenDebugging_thenInformationIsShown() {
16+
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());
24+
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();
33+
34+
assertThat(numberOf65PlusCustomers).isEqualTo(2);
35+
}
36+
}

0 commit comments

Comments
 (0)