|
| 1 | +package com.baeldung.convertiteratortolist; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
| 5 | +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.util.stream.StreamSupport; |
| 13 | + |
| 14 | +import org.apache.commons.collections4.IteratorUtils; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.common.collect.Lists; |
| 20 | + |
| 21 | +public class ConvertIteratorToListServiceUnitTest { |
| 22 | + |
| 23 | + Iterator<Integer> iterator; |
| 24 | + |
| 25 | + @Before |
| 26 | + public void setUp() throws Exception { |
| 27 | + iterator = Arrays.asList(1, 2, 3) |
| 28 | + .iterator(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() { |
| 33 | + |
| 34 | + List<Integer> actualList = new ArrayList<Integer>(); |
| 35 | + |
| 36 | + // Convert Iterator to List using while loop dsf |
| 37 | + while (iterator.hasNext()) { |
| 38 | + actualList.add(iterator.next()); |
| 39 | + } |
| 40 | + |
| 41 | + assertThat(actualList, hasSize(3)); |
| 42 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() { |
| 47 | + List<Integer> actualList = new ArrayList<Integer>(); |
| 48 | + |
| 49 | + // Convert Iterator to List using Java 8 |
| 50 | + iterator.forEachRemaining(actualList::add); |
| 51 | + |
| 52 | + assertThat(actualList, hasSize(3)); |
| 53 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenAnIterator_whenConvertIteratorToListJava8Stream_thenReturnAList() { |
| 58 | + |
| 59 | + // Convert iterator to iterable |
| 60 | + Iterable<Integer> iterable = () -> iterator; |
| 61 | + |
| 62 | + // Extract List from stream |
| 63 | + List<Integer> actualList = StreamSupport |
| 64 | + .stream(iterable.spliterator(), false) |
| 65 | + .collect(Collectors.toList()); |
| 66 | + |
| 67 | + assertThat(actualList, hasSize(3)); |
| 68 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenAnIterator_whenConvertIteratorToImmutableListWithGuava_thenReturnAList() { |
| 73 | + |
| 74 | + // Convert Iterator to an Immutable list using Guava library in Java |
| 75 | + List<Integer> actualList = ImmutableList.copyOf(iterator); |
| 76 | + |
| 77 | + assertThat(actualList, hasSize(3)); |
| 78 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenAnIterator_whenConvertIteratorToMutableListWithGuava_thenReturnAList() { |
| 83 | + |
| 84 | + // Convert Iterator to a mutable list using Guava library in Java |
| 85 | + List<Integer> actualList = Lists.newArrayList(iterator); |
| 86 | + |
| 87 | + assertThat(actualList, hasSize(3)); |
| 88 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenAnIterator_whenConvertIteratorToMutableListWithApacheCommons_thenReturnAList() { |
| 93 | + |
| 94 | + // Convert Iterator to a mutable list using Apache Commons library in Java |
| 95 | + List<Integer> actualList = IteratorUtils.toList(iterator); |
| 96 | + |
| 97 | + assertThat(actualList, hasSize(3)); |
| 98 | + assertThat(actualList, containsInAnyOrder(1, 2, 3)); |
| 99 | + } |
| 100 | +} |
0 commit comments