Bael 7163 (#15532)
* Create StreamFromEmptyList.java Initial Commit * Create StreamFromEmptyListUnitTest.java Initial commit of StreamFromEmptyListUnitTest. * Create README.md Adding README file. * Create pom.xml * Create pom.xml * Create REAME.md * Delete core-java-modules/core-java-streams-6/src/README.md * Delete core-java-modules/core-java-streams-6/src/pom.xml * Update pom.xml * Update pom.xml
This commit is contained in:
parent
c23c21e38e
commit
40dc851047
1
core-java-modules/core-java-streams-6/REAME.md
Normal file
1
core-java-modules/core-java-streams-6/REAME.md
Normal file
@ -0,0 +1 @@
|
||||
|
87
core-java-modules/core-java-streams-6/pom.xml
Normal file
87
core-java-modules/core-java-streams-6/pom.xml
Normal file
@ -0,0 +1,87 @@
|
||||
<?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-6</artifactId>
|
||||
<name>core-java-streams-6</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-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-streams-6</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>../core-java-streams-6/src/main</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<compilerArgument>-parameters</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>12</maven.compiler.source>
|
||||
<maven.compiler.target>12</maven.compiler.target>
|
||||
<vavr.version>0.10.2</vavr.version>
|
||||
<assertj-core.version>3.23.1</assertj-core.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,56 @@
|
||||
package com.baeldung.streams.emptylists;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StreamFromEmptyList {
|
||||
|
||||
public static void main(String[] args) {
|
||||
createStreamFromEmptyList();
|
||||
|
||||
List<String> nameList = getList();
|
||||
printNameLengths(nameList); // passing null
|
||||
printNameLengths((nameList == null) ? List.of() : nameList); // passing empty list
|
||||
|
||||
collectStreamOfEmptyListToAnotherList();
|
||||
}
|
||||
|
||||
private static void collectStreamOfEmptyListToAnotherList() {
|
||||
List<String> emptyList = new ArrayList<>();
|
||||
List<String> collectedList = emptyList.stream().collect(Collectors.toList());
|
||||
|
||||
System.out.println(collectedList.size() == 0);
|
||||
|
||||
collectedList = emptyList.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());
|
||||
System.out.println(collectedList.size() == 0);
|
||||
}
|
||||
|
||||
private static void createStreamFromEmptyList() {
|
||||
List<String> emptyList = new ArrayList<>();
|
||||
Stream<String> emptyStream = emptyList.stream();
|
||||
System.out.println(emptyStream.findAny().isEmpty());
|
||||
}
|
||||
|
||||
private static void printNameLengths(List<String> nameList) {
|
||||
// Without Stream - Traditional approach with null check
|
||||
if (nameList != null) {
|
||||
for (String name : nameList) {
|
||||
System.out.println("Length of " + name + ": " + name.length());
|
||||
}
|
||||
} else {
|
||||
System.out.println("List is null. Unable to process.");
|
||||
}
|
||||
|
||||
// With Stream - More concise approach
|
||||
Optional.ofNullable(nameList).ifPresent(list -> list.stream()
|
||||
.map(name -> "Length of " + name + ": " + name.length()).forEach(System.out::println));
|
||||
}
|
||||
|
||||
private static List<String> getList() {
|
||||
// This method may return null for demonstration purposes
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.streams.emptylists;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StreamFromEmptyListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEmptyList_whenConvertedToStream_thenResultantStreamIsEmpty() {
|
||||
List<String> emptyList = new ArrayList<>();
|
||||
Stream<String> emptyStream = emptyList.stream();
|
||||
assertTrue(emptyStream.findAny().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyList_whenCollectStreamOfEmptyListToAnotherList_thenResultantListIsEmpty() {
|
||||
List<String> emptyList = new ArrayList<>();
|
||||
List<String> collectedList = emptyList.stream().collect(Collectors.toList());
|
||||
assertTrue(collectedList.isEmpty());
|
||||
}
|
||||
}
|
@ -63,6 +63,7 @@
|
||||
<module>core-java-perf-2</module>
|
||||
<module>core-java-streams-4</module>
|
||||
<module>core-java-streams-5</module>
|
||||
<module>core-java-streams-6</module>
|
||||
<module>core-java-streams-collect</module>
|
||||
<module>core-java-streams-maps</module>
|
||||
<module>core-java-string-operations-3</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user