Skip to content

Commit 665bb01

Browse files
committed
Utility to concatenate streams into one stream
1 parent 267c5e5 commit 665bb01

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.nitorcreations.streams;
2+
3+
import java.util.stream.Stream;
4+
5+
public class NStreamsUtils {
6+
7+
@SafeVarargs
8+
public static <T> Stream<T> concat(Stream<T> ...streams) {
9+
return Stream.of(streams).reduce(Stream::concat).get();
10+
}
11+
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.nitorcreations.streams;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static java.util.Arrays.asList;
9+
import static java.util.stream.Collectors.toList;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class NStreamsUtilsTest {
13+
14+
@Test
15+
public void test_concatenation_empty_lists() {
16+
List<String> emptylist = new ArrayList<>();
17+
List<String> combinedList = NStreamsUtils.concat(emptylist.stream(), emptylist.stream())
18+
.collect(toList());
19+
assertThat(combinedList).isEmpty();
20+
}
21+
22+
@Test
23+
public void test_concatenation() {
24+
List<String> list1 = asList("1", "11");
25+
List<String> list2 = asList("2", "22");
26+
List<String> list3 = asList("3");
27+
28+
List<String> combinedList = NStreamsUtils.concat(list1.stream(), list2.stream(), list3.stream()).collect(toList());
29+
assertThat(combinedList).containsExactly("1","11","2","22","3");
30+
}
31+
}

0 commit comments

Comments
 (0)