BAEL-885 - How to merge two Java streams? (#1884)
Added examples using: * JDK8 * StreamEx library * JOOl library
This commit is contained in:
parent
994ac4ddbf
commit
b135086d07
@ -276,6 +276,16 @@
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>0.6.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jool</artifactId>
|
||||
<version>0.9.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import org.jooq.lambda.Seq;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class JoolMergeStreamsTest {
|
||||
@Test
|
||||
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
|
||||
Seq<Integer> seq1 = Seq.of(1, 3, 5);
|
||||
Seq<Integer> seq2 = Seq.of(2, 4, 6);
|
||||
|
||||
Seq<Integer> resultingSeq = seq1.append(seq2);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
|
||||
resultingSeq.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeStreams_whenAppendingAndPrependingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
Seq<String> seq = Seq.of("foo", "bar");
|
||||
Seq<String> openingBracketSeq = Seq.of("[");
|
||||
Seq<String> closingBracketSeq = Seq.of("]");
|
||||
|
||||
Seq<String> resultingStream = seq.append(closingBracketSeq)
|
||||
.prepend(openingBracketSeq);
|
||||
|
||||
Assert.assertEquals(Arrays.asList("[", "foo", "bar", "]"),
|
||||
resultingStream.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MergeStreamsTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
|
||||
Stream<Integer> stream1 = Stream.of(1, 3, 5);
|
||||
Stream<Integer> stream2 = Stream.of(2, 4, 6);
|
||||
|
||||
Stream<Integer> resultingStream = Stream.concat(stream1,
|
||||
stream2);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
|
||||
resultingStream.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
Stream<Integer> stream1 = Stream.of(1, 3, 5);
|
||||
Stream<Integer> stream2 = Stream.of(2, 4, 6);
|
||||
Stream<Integer> stream3 = Stream.of(18, 15, 36);
|
||||
|
||||
Stream<Integer> resultingStream = Stream.concat(Stream.concat(stream1,
|
||||
stream2),
|
||||
stream3);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36),
|
||||
resultingStream.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFourStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
Stream<Integer> stream1 = Stream.of(1, 3, 5);
|
||||
Stream<Integer> stream2 = Stream.of(2, 4, 6);
|
||||
Stream<Integer> stream3 = Stream.of(18, 15, 36);
|
||||
Stream<Integer> stream4 = Stream.of(99);
|
||||
|
||||
Stream<Integer> resultingStream = Stream.of(stream1, stream2, stream3, stream4).flatMap(Function.identity());
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99),
|
||||
resultingStream.collect(Collectors.toList()));
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StreamExMergeStreamsTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
|
||||
StreamEx<Integer> stream1 = StreamEx.of(1, 3, 5);
|
||||
StreamEx<Integer> stream2 = StreamEx.of(2, 4, 6);
|
||||
|
||||
StreamEx<Integer> resultingStream = stream1.append(stream2);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
|
||||
resultingStream.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFourStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
StreamEx<Integer> stream1 = StreamEx.of(1, 3, 5);
|
||||
StreamEx<Integer> stream2 = StreamEx.of(2, 4, 6);
|
||||
StreamEx<Integer> stream3 = StreamEx.of(18, 15, 36);
|
||||
StreamEx<Integer> stream4 = StreamEx.of(99);
|
||||
|
||||
StreamEx<Integer> resultingStream = stream1.append(stream2)
|
||||
.append(stream3)
|
||||
.append(stream4);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99),
|
||||
resultingStream.toList());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeStreams_whenAppendingAndPrependingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
StreamEx<String> stream1 = StreamEx.of("foo", "bar");
|
||||
StreamEx<String> openingBracketStream = StreamEx.of("[");
|
||||
StreamEx<String> closingBracketStream = StreamEx.of("]");
|
||||
|
||||
StreamEx<String> resultingStream = stream1.append(closingBracketStream)
|
||||
.prepend(openingBracketStream);
|
||||
|
||||
assertEquals(Arrays.asList("[", "foo", "bar", "]"),
|
||||
resultingStream.toList());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user