Skip to content

Commit be171f7

Browse files
committed
iterable to collection
1 parent 765f355 commit be171f7

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.baeldung.java.collections;
2+
3+
import static org.hamcrest.Matchers.contains;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
import java.util.Spliterator;
11+
import java.util.Spliterators;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.StreamSupport;
14+
15+
import org.apache.commons.collections4.IterableUtils;
16+
import org.apache.commons.collections4.IteratorUtils;
17+
import org.junit.Test;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.common.collect.Lists;
21+
22+
public class IterableToCollectionUnitTest {
23+
24+
Iterable<String> iterable = Arrays.asList("john", "tom", "jane");
25+
Iterator<String> iterator = iterable.iterator();
26+
27+
@Test
28+
public void whenConvertIterableToListUsingJava_thenSuccess() {
29+
List<String> result = new ArrayList<String>();
30+
for (String str : iterable) {
31+
result.add(str);
32+
}
33+
34+
assertThat(result, contains("john", "tom", "jane"));
35+
}
36+
37+
@Test
38+
public void whenConvertIterableToListUsingJava8_thenSuccess() {
39+
List<String> result = new ArrayList<String>();
40+
iterable.forEach(result::add);
41+
42+
assertThat(result, contains("john", "tom", "jane"));
43+
}
44+
45+
@Test
46+
public void whenConvertIterableToListUsingJava8WithSpliterator_thenSuccess() {
47+
List<String> result = StreamSupport.stream(iterable.spliterator(), false)
48+
.collect(Collectors.toList());
49+
50+
assertThat(result, contains("john", "tom", "jane"));
51+
}
52+
53+
@Test
54+
public void whenConvertIterableToListUsingGuava_thenSuccess() {
55+
List<String> result = Lists.newArrayList(iterable);
56+
57+
assertThat(result, contains("john", "tom", "jane"));
58+
}
59+
60+
@Test
61+
public void whenConvertIterableToImmutableListUsingGuava_thenSuccess() {
62+
List<String> result = ImmutableList.copyOf(iterable);
63+
64+
assertThat(result, contains("john", "tom", "jane"));
65+
}
66+
67+
@Test
68+
public void whenConvertIterableToListUsingApacheCommons_thenSuccess() {
69+
List<String> result = IterableUtils.toList(iterable);
70+
71+
assertThat(result, contains("john", "tom", "jane"));
72+
}
73+
74+
// ======================== Iterator
75+
76+
@Test
77+
public void whenConvertIteratorToListUsingJava_thenSuccess() {
78+
List<String> result = new ArrayList<String>();
79+
while (iterator.hasNext()) {
80+
result.add(iterator.next());
81+
}
82+
83+
assertThat(result, contains("john", "tom", "jane"));
84+
}
85+
86+
@Test
87+
public void whenConvertIteratorToListUsingJava8_thenSuccess() {
88+
List<String> result = new ArrayList<String>();
89+
iterator.forEachRemaining(result::add);
90+
91+
assertThat(result, contains("john", "tom", "jane"));
92+
}
93+
94+
@Test
95+
public void whenConvertIteratorToListUsingJava8WithSpliterator_thenSuccess() {
96+
List<String> result = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
97+
.collect(Collectors.toList());
98+
99+
assertThat(result, contains("john", "tom", "jane"));
100+
}
101+
102+
@Test
103+
public void whenConvertIteratorToListUsingGuava_thenSuccess() {
104+
List<String> result = Lists.newArrayList(iterator);
105+
106+
assertThat(result, contains("john", "tom", "jane"));
107+
}
108+
109+
@Test
110+
public void whenConvertIteratorToImmutableListUsingGuava_thenSuccess() {
111+
List<String> result = ImmutableList.copyOf(iterator);
112+
113+
assertThat(result, contains("john", "tom", "jane"));
114+
}
115+
116+
@Test
117+
public void whenConvertIteratorToListUsingApacheCommons_thenSuccess() {
118+
List<String> result = IteratorUtils.toList(iterator);
119+
120+
assertThat(result, contains("john", "tom", "jane"));
121+
}
122+
}

0 commit comments

Comments
 (0)