|
| 1 | +package org.baeldung.java.collections; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.List; |
| 10 | +import java.util.NoSuchElementException; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import com.google.common.collect.Iterables; |
| 18 | +import com.google.common.collect.Lists; |
| 19 | + |
| 20 | +public class CollectionsConcatenateUnitTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenUsingJava8_whenConcatenatingUsingConcat_thenCorrect() { |
| 24 | + Collection<String> collectionA = asList("S", "T"); |
| 25 | + Collection<String> collectionB = asList("U", "V"); |
| 26 | + Collection<String> collectionC = asList("W", "X"); |
| 27 | + |
| 28 | + Stream<String> combinedStream = Stream.concat(Stream.concat(collectionA.stream(), collectionB.stream()), collectionC.stream()); |
| 29 | + Collection<String> collectionCombined = combinedStream.collect(Collectors.toList()); |
| 30 | + |
| 31 | + Assert.assertEquals(asList("S", "T", "U", "V", "W", "X"), collectionCombined); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenUsingJava8_whenConcatenatingUsingflatMap_thenCorrect() { |
| 36 | + Collection<String> collectionA = asList("S", "T"); |
| 37 | + Collection<String> collectionB = asList("U", "V"); |
| 38 | + |
| 39 | + Stream<String> combinedStream = Stream.of(collectionA, collectionB).flatMap(Collection::stream); |
| 40 | + Collection<String> collectionCombined = combinedStream.collect(Collectors.toList()); |
| 41 | + |
| 42 | + Assert.assertEquals(asList("S", "T", "U", "V"), collectionCombined); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenUsingGuava_whenConcatenatingUsingIterables_thenCorrect() { |
| 47 | + Collection<String> collectionA = asList("S", "T"); |
| 48 | + Collection<String> collectionB = asList("U", "V"); |
| 49 | + |
| 50 | + Iterable<String> combinedIterables = Iterables.unmodifiableIterable(Iterables.concat(collectionA, collectionB)); |
| 51 | + Collection<String> collectionCombined = Lists.newArrayList(combinedIterables); |
| 52 | + |
| 53 | + Assert.assertEquals(asList("S", "T", "U", "V"), collectionCombined); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenUsingJava7_whenConcatenatingUsingIterables_thenCorrect() { |
| 58 | + Collection<String> collectionA = asList("S", "T"); |
| 59 | + Collection<String> collectionB = asList("U", "V"); |
| 60 | + |
| 61 | + Iterable<String> combinedIterables = concat(collectionA, collectionB); |
| 62 | + Collection<String> collectionCombined = makeListFromIterable(combinedIterables); |
| 63 | + Assert.assertEquals(Arrays.asList("S", "T", "U", "V"), collectionCombined); |
| 64 | + } |
| 65 | + |
| 66 | + public static <E> Iterable<E> concat(Iterable<? extends E> list1, Iterable<? extends E> list2) { |
| 67 | + return new Iterable<E>() { |
| 68 | + public Iterator<E> iterator() { |
| 69 | + return new Iterator<E>() { |
| 70 | + protected Iterator<? extends E> listIterator = list1.iterator(); |
| 71 | + protected Boolean checkedHasNext; |
| 72 | + protected E nextValue; |
| 73 | + private boolean startTheSecond; |
| 74 | + |
| 75 | + public void theNext() { |
| 76 | + if (listIterator.hasNext()) { |
| 77 | + checkedHasNext = true; |
| 78 | + nextValue = listIterator.next(); |
| 79 | + } else if (startTheSecond) |
| 80 | + checkedHasNext = false; |
| 81 | + else { |
| 82 | + startTheSecond = true; |
| 83 | + listIterator = list2.iterator(); |
| 84 | + theNext(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public boolean hasNext() { |
| 89 | + if (checkedHasNext == null) |
| 90 | + theNext(); |
| 91 | + return checkedHasNext; |
| 92 | + } |
| 93 | + |
| 94 | + public E next() { |
| 95 | + if (!hasNext()) |
| 96 | + throw new NoSuchElementException(); |
| 97 | + checkedHasNext = null; |
| 98 | + return nextValue; |
| 99 | + } |
| 100 | + |
| 101 | + public void remove() { |
| 102 | + listIterator.remove(); |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + public static <E> List<E> makeListFromIterable(Iterable<E> iter) { |
| 110 | + List<E> list = new ArrayList<E>(); |
| 111 | + for (E item : iter) { |
| 112 | + list.add(item); |
| 113 | + } |
| 114 | + return list; |
| 115 | + } |
| 116 | +} |
0 commit comments