From 44dfb1a07679d768d8a067d2b58c4545d437a64a Mon Sep 17 00:00:00 2001 From: Antonio Moreno Date: Mon, 17 Jun 2019 00:00:29 +0100 Subject: [PATCH] BAEL-2934 IntStreams conversions (cherry picked from commit 11abb325ecb2f299c318d82952e7718bab03510c) --- .../IntStreamsConversionsUnitTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 java-streams/src/test/java/com/baeldung/intstreams/conversion/IntStreamsConversionsUnitTest.java diff --git a/java-streams/src/test/java/com/baeldung/intstreams/conversion/IntStreamsConversionsUnitTest.java b/java-streams/src/test/java/com/baeldung/intstreams/conversion/IntStreamsConversionsUnitTest.java new file mode 100644 index 0000000000..88579cd97c --- /dev/null +++ b/java-streams/src/test/java/com/baeldung/intstreams/conversion/IntStreamsConversionsUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.intstreams.conversion; + +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import static org.assertj.core.api.Assertions.assertThat; + +public class IntStreamsConversionsUnitTest { + + @Test + public void intStreamToArray() { + int[] first50EvenNumbers = IntStream.iterate(0, i -> i + 2) + .limit(50) + .toArray(); + + assertThat(first50EvenNumbers).hasSize(50); + assertThat(first50EvenNumbers[2]).isEqualTo(4); + } + + @Test + public void intStreamToList() { + List first50IntegerNumbers = IntStream.range(0, 50) + .boxed() + .collect(Collectors.toList()); + + assertThat(first50IntegerNumbers).hasSize(50); + assertThat(first50IntegerNumbers.get(2)).isEqualTo(2); + } + + @Test + public void intStreamToString() { + String first3numbers = IntStream.range(0, 3) + .mapToObj(String::valueOf) + .collect(Collectors.joining(", ", "[", "]")); + + assertThat(first3numbers).isEqualTo("[0, 1, 2]"); + } +}