BAEL-602 add example of join using only Stream API

This commit is contained in:
Tomasz Lelek 2017-02-18 22:40:15 +01:00
parent 306eb4ee96
commit e35c452ad1
1 changed files with 20 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
@ -32,14 +33,22 @@ public class JOOLTest {
assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5));
}
@Test
public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() {
//given
Stream<Integer> left = Arrays.asList(1, 2, 4).stream();
Stream<Integer> right = Arrays.asList(1, 2, 3).stream();
//when
List<Integer> rightCollected = right.collect(Collectors.toList());
List<Integer> collect = left.filter(rightCollected::contains).collect(Collectors.toList());
//then
assertEquals(collect, Arrays.asList(1, 2));
}
@Test
public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() {
assertEquals(
Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(),
Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
);
assertEquals(
Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
Arrays.asList(tuple(1, 1), tuple(2, 2))
@ -55,6 +64,11 @@ public class JOOLTest {
Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3))
);
assertEquals(
Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(),
Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
);
}
@Test