Skip to content

Commit e35c452

Browse files
author
Tomasz Lelek
committed
BAEL-602 add example of join using only Stream API
1 parent 306eb4e commit e35c452

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

‎jooq/src/test/java/com/baeldung/JOOLTest.java‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
1314

1415
import static junit.framework.Assert.assertTrue;
1516
import static junit.framework.TestCase.assertEquals;
@@ -33,13 +34,21 @@ public void givenSeq_whenCheckContains_shouldReturnTrue() {
3334
}
3435

3536
@Test
36-
public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() {
37-
assertEquals(
38-
Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(),
39-
Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
40-
);
37+
public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() {
38+
//given
39+
Stream<Integer> left = Arrays.asList(1, 2, 4).stream();
40+
Stream<Integer> right = Arrays.asList(1, 2, 3).stream();
41+
42+
//when
43+
List<Integer> rightCollected = right.collect(Collectors.toList());
44+
List<Integer> collect = left.filter(rightCollected::contains).collect(Collectors.toList());
4145

46+
//then
47+
assertEquals(collect, Arrays.asList(1, 2));
48+
}
4249

50+
@Test
51+
public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() {
4352
assertEquals(
4453
Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
4554
Arrays.asList(tuple(1, 1), tuple(2, 2))
@@ -55,6 +64,11 @@ public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() {
5564
Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
5665
Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3))
5766
);
67+
68+
assertEquals(
69+
Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(),
70+
Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
71+
);
5872
}
5973

6074
@Test

0 commit comments

Comments
 (0)