[can-collect-return-null] Can Stream.collect() Return Null Value? (#13573)

* [can-collect-return-null] Can Stream.collect() Return Null Value?

* [can-collect-return-null] remove the .gitignore file
This commit is contained in:
Kai Yuan 2023-03-11 01:04:00 +01:00 committed by GitHub
parent 65c9b520ee
commit 911df283d4
4 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,2 @@
## Relevant Articles:

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-streams-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-streams-collect</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<assertj.version>3.23.1</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,92 @@
package com.baeldung.collect.nullable;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collector;
import org.junit.jupiter.api.Test;
public class CanCollectReturnNullUnitTest {
private final List<String> LANGUAGES = Arrays.asList("Kotlin", null, null, "Java", "Python", "Rust");
@Test
void givenAStreamWithNullElements_whenCollect_shouldReturnNotNull() {
List<String> result = LANGUAGES.stream()
.filter(Objects::isNull)
.collect(toList());
assertNotNull(result);
assertEquals(Arrays.asList(null, null), result);
}
@Test
void givenAStreamWithNullElements_whenCollectEmptyStream_shouldReturnNotNull() {
List<String> result = LANGUAGES.stream()
.filter(s -> s != null && s.length() == 1)
.collect(toList());
assertNotNull(result);
assertTrue(result.isEmpty());
Map<Character, String> result2 = LANGUAGES.stream()
.filter(s -> s != null && s.length() == 1)
.collect(toMap(s -> s.charAt(0), Function.identity()));
assertNotNull(result2);
assertTrue(result2.isEmpty());
Map<Character, List<String>> result3 = LANGUAGES.stream()
.filter(s -> s != null && s.length() == 1)
.collect(groupingBy(s -> s.charAt(0)));
assertNotNull(result3);
assertTrue(result3.isEmpty());
}
@Test
void givenAStream_whenCollectByEmptyToNullCollector_shouldReturnExpectedResults() {
Collector<String, ArrayList<String>, ArrayList<String>> emptyListToNullCollector = Collector.of(ArrayList::new, ArrayList::add, (a, b) -> {
a.addAll(b);
return a;
}, a -> a.isEmpty() ? null : a);
List<String> notNullResult = LANGUAGES.stream()
.filter(Objects::isNull)
.collect(emptyListToNullCollector);
assertNotNull(notNullResult);
assertEquals(Arrays.asList(null, null), notNullResult);
List<String> nullResult = LANGUAGES.stream()
.filter(s -> s != null && s.length() == 1)
.collect(emptyListToNullCollector);
assertNull(nullResult);
}
@Test
void givenAStream_whenCollectByCollectingAndThen_shouldReturnExpectedResults() {
List<String> notNullResult = LANGUAGES.stream()
.filter(Objects::nonNull)
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
assertNotNull(notNullResult);
assertEquals(Arrays.asList("Kotlin", "Java", "Python", "Rust"), notNullResult);
//the result list becomes immutable
assertThrows(UnsupportedOperationException.class, () -> notNullResult.add("Oops"));
List<String> nullResult = LANGUAGES.stream()
.filter(s -> s != null && s.length() == 1)
.collect(collectingAndThen(toList(), strings -> strings.isEmpty() ? null : strings));
assertNull(nullResult);
}
}

View File

@ -121,6 +121,7 @@
<module>core-java-streams-2</module>
<module>core-java-streams-3</module>
<module>core-java-streams-maps</module>
<module>core-java-streams-collect</module>
<module>core-java-string-algorithms</module>
<module>core-java-string-algorithms-2</module>
<module>core-java-string-apis</module>