From be171f7f59cad31d406c9e44da5aee4a185457cf Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 13 Jun 2019 21:38:53 +0300 Subject: [PATCH] iterable to collection --- .../IterableToCollectionUnitTest.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 java-collections-conversions/src/test/java/org/baeldung/java/collections/IterableToCollectionUnitTest.java diff --git a/java-collections-conversions/src/test/java/org/baeldung/java/collections/IterableToCollectionUnitTest.java b/java-collections-conversions/src/test/java/org/baeldung/java/collections/IterableToCollectionUnitTest.java new file mode 100644 index 0000000000..f2c80429d1 --- /dev/null +++ b/java-collections-conversions/src/test/java/org/baeldung/java/collections/IterableToCollectionUnitTest.java @@ -0,0 +1,122 @@ +package org.baeldung.java.collections; + +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.apache.commons.collections4.IterableUtils; +import org.apache.commons.collections4.IteratorUtils; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +public class IterableToCollectionUnitTest { + + Iterable iterable = Arrays.asList("john", "tom", "jane"); + Iterator iterator = iterable.iterator(); + + @Test + public void whenConvertIterableToListUsingJava_thenSuccess() { + List result = new ArrayList(); + for (String str : iterable) { + result.add(str); + } + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingJava8_thenSuccess() { + List result = new ArrayList(); + iterable.forEach(result::add); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingJava8WithSpliterator_thenSuccess() { + List result = StreamSupport.stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingGuava_thenSuccess() { + List result = Lists.newArrayList(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToImmutableListUsingGuava_thenSuccess() { + List result = ImmutableList.copyOf(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingApacheCommons_thenSuccess() { + List result = IterableUtils.toList(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + // ======================== Iterator + + @Test + public void whenConvertIteratorToListUsingJava_thenSuccess() { + List result = new ArrayList(); + while (iterator.hasNext()) { + result.add(iterator.next()); + } + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingJava8_thenSuccess() { + List result = new ArrayList(); + iterator.forEachRemaining(result::add); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingJava8WithSpliterator_thenSuccess() { + List result = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) + .collect(Collectors.toList()); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingGuava_thenSuccess() { + List result = Lists.newArrayList(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToImmutableListUsingGuava_thenSuccess() { + List result = ImmutableList.copyOf(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingApacheCommons_thenSuccess() { + List result = IteratorUtils.toList(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } +}