diff --git a/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java b/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java new file mode 100644 index 0000000000..ba7339fc7e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java @@ -0,0 +1,44 @@ +package com.baeldung.java.conversion; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.junit.Assert; +import org.junit.Test; + +public class IterableStreamConversionTest { + + @Test + public void givenIterable_whenConvertedToStream_thenTrue() { + String[] names = { "Testing", "Iterable", "conversion", "to", "Stream" }; + StreamIterable iterable = new StreamIterable<>(names); + Assert.assertTrue(StreamSupport.stream(iterable.spliterator(), false) instanceof Stream); + } + + @Test + public void whenConvertedToList_thenCorrect() { + String[] names = { "Testing", "Iterable", "conversion", "to", "Stream" }; + StreamIterable iterable = new StreamIterable<>(names); + Stream convertedStream = StreamSupport.stream(iterable.spliterator(), false); + Assert.assertTrue(convertedStream.map(String::toUpperCase) + .collect(Collectors.toList()) instanceof List); + } +} + +class StreamIterable implements Iterable { + private List list; + + public StreamIterable(T[] array) { + this.list = Arrays.asList(array); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + +}