diff --git a/core-java-modules/core-java-16/README.md b/core-java-modules/core-java-16/README.md new file mode 100644 index 0000000000..760513189f --- /dev/null +++ b/core-java-modules/core-java-16/README.md @@ -0,0 +1,3 @@ +### Relevant articles: + +- [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection) diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml new file mode 100644 index 0000000000..230e342f01 --- /dev/null +++ b/core-java-modules/core-java-16/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + core-java-16 + 0.1.0-SNAPSHOT + core-java-16 + jar + http://maven.apache.org + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + + + + + 16 + 16 + 3.6.1 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToImmutableUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToImmutableUnitTest.java new file mode 100644 index 0000000000..afd7369d8d --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToImmutableUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.streams; + +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StreamToImmutableUnitTest { + + @Test + public void whenUsingStreamToList_thenReturnImmutableList() { + + List immutableList = Stream.of("a", "b", "c", "d") + .toList(); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + immutableList.add("e"); + }); + + } + +}