Java8写一个共通方法,将两个类型不同的stream输入按多个字段进行匹配,并返回匹配成功的两个输出数据。已知stream输入已按匹配字段排好序,为了提高效率,使用了迭代器和循环来避免嵌套循环。要求当StreamA中匹配成功的两条key相等时,只返回最后一条数据
时间: 2023-06-16 17:04:27 浏览: 105
Java8中Stream使用的一个注意事项
5星 · 资源好评率100%
可以使用以下方法来实现:
```java
public static <T, U, K extends Comparable<? super K>> Optional<Pair<T, U>> matchStreamsByMultipleKeys(Stream<T> streamA, Stream<U> streamB, Function<T, K[]> keyExtractorA, Function<U, K[]> keyExtractorB) {
Iterator<T> iterA = streamA.iterator();
Iterator<U> iterB = streamB.iterator();
T currentA = null;
U currentB = null;
K[] keysA = null;
K[] keysB = null;
while (iterA.hasNext() && iterB.hasNext()) {
if (currentA == null) {
currentA = iterA.next();
keysA = keyExtractorA.apply(currentA);
}
if (currentB == null) {
currentB = iterB.next();
keysB = keyExtractorB.apply(currentB);
}
int compare = compareKeys(keysA, keysB);
if (compare == 0) {
// Match found
Optional<Pair<T, U>> result = Optional.of(new Pair<>(currentA, currentB));
// Skip to the last match in streamA
while (iterA.hasNext()) {
T nextA = iterA.next();
K[] nextKeysA = keyExtractorA.apply(nextA);
if (compareKeys(nextKeysA, keysB) == 0) {
currentA = nextA;
keysA = nextKeysA;
result = Optional.empty();
} else {
currentA = nextA;
keysA = nextKeysA;
break;
}
}
// Skip to the next match in streamB
while (iterB.hasNext()) {
U nextB = iterB.next();
K[] nextKeysB = keyExtractorB.apply(nextB);
if (compareKeys(keysA, nextKeysB) == 0) {
currentB = nextB;
keysB = nextKeysB;
result = Optional.empty();
} else {
currentB = nextB;
keysB = nextKeysB;
break;
}
}
return result;
} else if (compare < 0) {
currentA = null;
keysA = null;
} else {
currentB = null;
keysB = null;
}
}
return Optional.empty();
}
private static <K extends Comparable<? super K>> int compareKeys(K[] keysA, K[] keysB) {
for (int i = 0; i < Math.min(keysA.length, keysB.length); i++) {
int compare = keysA[i].compareTo(keysB[i]);
if (compare != 0) {
return compare;
}
}
return Integer.compare(keysA.length, keysB.length);
}
```
这个方法使用了两个迭代器来遍历两个输入流,同时使用两个函数来提取每个元素的匹配键。在迭代器循环中,我们比较当前A和B的键,如果它们匹配,则返回匹配对。否则,我们将当前指针移到下一个不匹配的元素,直到下一个匹配的元素出现。如果在StreamA中有多个匹配的元素,则我们只返回最后一个匹配的元素,因为它们是按照匹配字段排序的。
注意,为了提高效率,我们使用了一个简单的循环来比较匹配键,而不是使用Java 8的Stream API。这是因为Stream API需要创建中间流对象,这可能会影响性能。
阅读全文