[BAEL-3392] Add code samples for BAEL-3392 Debugging Java 8 Streams with IntelliJ

This commit is contained in:
Martin van Wingerden
2019-10-29 16:17:28 +01:00
parent 18c6a1a0ab
commit b264937ac6
3 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package com.baeldung.streams.debug.entity;
public class Customer {
private final String name;
private final int age;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.streams.debug;
import org.junit.Test;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
public class Example1 {
@Test
public void whenDebugging_thenInformationIsShown() {
int[] listOutputSorted = IntStream.of(-3, 10, -4, 1, 3)
.sorted()
.toArray();
assertThat(listOutputSorted).isSorted();
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.streams.debug;
import com.baeldung.streams.debug.entity.Customer;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
public class Example2 {
@Test
public void whenDebugging_thenInformationIsShown() {
List<Optional<Customer>> customers = Arrays.asList(
Optional.of(new Customer("John P.", 15)),
Optional.of(new Customer("Sarah M.", 78)),
Optional.empty(),
Optional.of(new Customer("Mary T.", 20)),
Optional.empty(),
Optional.of(new Customer("Florian G.", 89)),
Optional.empty());
long numberOf65PlusCustomers = customers
.stream()
.flatMap(c -> c
.map(Stream::of)
.orElseGet(Stream::empty))
.mapToInt(Customer::getAge)
.filter(c -> c > 65)
.count();
assertThat(numberOf65PlusCustomers).isEqualTo(2);
}
}