[JAVA-26432] Update "Introduction to Java 8 Streams" article Clean up (#15038)

This commit is contained in:
panos-kakos 2023-11-04 10:01:25 +00:00 committed by GitHub
parent df78df7a81
commit 1baaca3e37
1 changed files with 38 additions and 23 deletions

View File

@ -1,7 +1,11 @@
package com.baeldung.streams;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -11,14 +15,12 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*;
class Java8StreamsUnitTest {
public class Java8StreamsUnitTest {
private static List<String> list;
private List<String> list;
@Before
public void init() {
@BeforeAll
public static void init() {
list = new ArrayList<>();
list.add("One");
list.add("OneAndOnly");
@ -42,60 +44,73 @@ public class Java8StreamsUnitTest {
Stream<String> streamOf = Stream.of("a", "b", "c");
assertEquals(streamOf.count(), 3);
long count = list.stream().distinct().count();
long count = list.stream()
.distinct()
.count();
assertEquals(count, 9);
}
@Test
public void checkStreamCount_whenOperationFilter_thanCorrect() {
Stream<String> streamFilter = list.stream().filter(element -> element.isEmpty());
void checkStreamCount_whenOperationFilter_thanCorrect() {
Stream<String> streamFilter = list.stream()
.filter(element -> element.isEmpty());
assertEquals(streamFilter.count(), 2);
}
@Test
public void checkStreamCount_whenOperationMap_thanCorrect() {
void checkStreamCount_whenOperationMap_thanCorrect() {
List<String> uris = new ArrayList<>();
uris.add("C:\\My.txt");
Stream<Path> streamMap = uris.stream().map(uri -> Paths.get(uri));
Stream<Path> streamMap = uris.stream()
.map(uri -> Paths.get(uri));
assertEquals(streamMap.count(), 1);
List<Detail> details = new ArrayList<>();
details.add(new Detail());
details.add(new Detail());
Stream<String> streamFlatMap = details.stream().flatMap(detail -> detail.getParts().stream());
Stream<String> streamFlatMap = details.stream()
.flatMap(detail -> detail.getParts()
.stream());
assertEquals(streamFlatMap.count(), 4);
}
@Test
public void checkStreamCount_whenOperationMatch_thenCorrect() {
boolean isValid = list.stream().anyMatch(element -> element.contains("h"));
boolean isValidOne = list.stream().allMatch(element -> element.contains("h"));
boolean isValidTwo = list.stream().noneMatch(element -> element.contains("h"));
void checkStreamCount_whenOperationMatch_thenCorrect() {
boolean isValid = list.stream()
.anyMatch(element -> element.contains("h"));
boolean isValidOne = list.stream()
.allMatch(element -> element.contains("h"));
boolean isValidTwo = list.stream()
.noneMatch(element -> element.contains("h"));
assertTrue(isValid);
assertFalse(isValidOne);
assertFalse(isValidTwo);
}
@Test
public void checkStreamReducedValue_whenOperationReduce_thenCorrect() {
void checkStreamReducedValue_whenOperationReduce_thenCorrect() {
List<Integer> integers = new ArrayList<>();
integers.add(1);
integers.add(1);
integers.add(1);
Integer reduced = integers.stream().reduce(23, (a, b) -> a + b);
Integer reduced = integers.stream()
.reduce(23, (a, b) -> a + b);
assertTrue(reduced == 26);
}
@Test
public void checkStreamContains_whenOperationCollect_thenCorrect() {
List<String> resultList = list.stream().map(element -> element.toUpperCase()).collect(Collectors.toList());
void checkStreamContains_whenOperationCollect_thenCorrect() {
List<String> resultList = list.stream()
.map(element -> element.toUpperCase())
.collect(Collectors.toList());
assertEquals(resultList.size(), list.size());
assertTrue(resultList.contains(""));
}
@Test
public void checkParallelStream_whenDoWork() {
list.parallelStream().forEach(element -> doWork(element));
list.parallelStream()
.forEach(element -> doWork(element));
}
private void doWork(String string) {