Skip to content

Commit 4354bb0

Browse files
committed
feat: Join支持key为null的情况
1 parent 29da518 commit 4354bb0

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.lokic</groupId>
88
<artifactId>java-plus</artifactId>
9-
<version>0.1.0</version>
9+
<version>0.1.1</version>
1010
<name>java-plus</name>
1111
<description>Provide some useful extensions on the basis of java8 to make java more usable.</description>
1212
<url>https://github.com/lokic/java-plus</url>

src/main/java/com/github/lokic/javaplus/Join.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import com.github.lokic.javaplus.tuple.Tuple;
44
import com.github.lokic.javaplus.tuple.Tuple2;
55

6-
import java.util.ArrayList;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Objects;
6+
import java.util.*;
107
import java.util.function.Function;
118
import java.util.function.Predicate;
129
import java.util.stream.Collector;
@@ -71,15 +68,25 @@ public static class JoinType<T1, T2> {
7168
private final Stream<Tuple2<T1, T2>> rightWrappedStream;
7269
private final Predicate<Tuple2<T1, T2>> joinMatcher;
7370

74-
public JoinType(Stream<T1> left, Stream<T2> right, Predicate<Tuple2<T1, T2>> joinMatcher) {
71+
private JoinType(Stream<T1> left, Stream<T2> right, Predicate<Tuple2<T1, T2>> joinMatcher) {
7572
this.leftWrappedStream = left.map(l -> Tuple.of(Objects.requireNonNull(l), null));
7673
this.rightWrappedStream = right.map(r -> Tuple.of(null, Objects.requireNonNull(r)));
7774
this.joinMatcher = joinMatcher;
7875
}
7976

8077
public <K> JoinStream<T1, T2> on(Function<T1, K> leftKey, Function<T2, K> rightKey) {
8178
Stream<Tuple2<T1, T2>> stream = Stream.concat(leftWrappedStream, rightWrappedStream)
82-
.collect(Collectors.groupingBy(t -> matchKey(t, leftKey, rightKey)))
79+
.collect(Collectors.toMap(
80+
t -> matchKey(t, leftKey, rightKey),
81+
Collections::singletonList,
82+
(a, b) -> {
83+
List<Tuple2<T1, T2>> li = new ArrayList<>();
84+
li.addAll(a);
85+
li.addAll(b);
86+
return li;
87+
},
88+
LinkedHashMap::new
89+
))
8390
.values()
8491
.stream()
8592
.flatMap(this::cartesian)

src/test/java/com/github/lokic/javaplus/JoinTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,29 @@ public void test_joinStream_leftOuterJoin() {
106106
Tuple.of(3, "3", null),
107107
Tuple.of(4, null, null));
108108
}
109+
110+
@Test
111+
public void test_nullKey() {
112+
List<Tuple2<Integer, Integer>> list = Join.leftOuterJoin(Stream.of(new KeyBox(1), new KeyBox(2), new KeyBox(3), new KeyBox(4), new KeyBox(null)), Stream.of(2, 3, 5))
113+
.on(KeyBox::getKey, Function.identity())
114+
.stream()
115+
.map(t -> Tuple.of(t.getT1().getKey(), t.getT2()))
116+
.collect(Collectors.toList());
117+
Assertions.assertThat(list)
118+
.containsExactly(Tuple.of(1, null), Tuple.of(2, 2), Tuple.of(3, 3),
119+
Tuple.of(4, null), Tuple.of(null, null));
120+
121+
}
122+
123+
public static class KeyBox {
124+
private final Integer key;
125+
126+
public KeyBox(Integer key) {
127+
this.key = key;
128+
}
129+
130+
public Integer getKey() {
131+
return key;
132+
}
133+
}
109134
}

0 commit comments

Comments
 (0)