3 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊
即“如何將兩個(gè) IntStream 合并為一個(gè)”。
你的另一個(gè)問題是“如何在Stream<T>不覆蓋equals()andhashCode()方法的情況下合并兩個(gè)?” 可以使用toMap收集器完成,即假設(shè)您不希望結(jié)果為Stream<T>. 例子:
Stream.concat(stream1, stream2)
.collect(Collectors.toMap(Student::getNo,
Function.identity(),
(l, r) -> l,
LinkedHashMap::new)
).values();
如果你想要結(jié)果,Stream<T>那么你可以這樣做:
Stream.concat(stream1, stream2)
.collect(Collectors.collectingAndThen(
Collectors.toMap(Student::getNo,
Function.identity(),
(l, r) -> l,
LinkedHashMap::new),
f -> f.values().stream()));
這可能不像它那樣有效,但它是另一種返回 a 的方法,Stream<T>其中T項(xiàng)目都是不同的,但不使用覆蓋equals,hashcode正如您所提到的。

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
對(duì)于第一個(gè)問題,您可以使用“flatMap”
IntStream stream1 = Arrays.stream(new int[] {13, 1, 3, 5, 7, 9});
IntStream stream2 = Arrays.stream(new int[] {1, 2, 6, 14, 8, 10, 12});
List<Integer> result = Stream.of(stream1, stream2).flatMap(IntStream::boxed)
.collect(Collectors.toList());
//result={13, 1, 3, 5, 7, 9, 1, 2, 6, 14, 8, 10, 12}
添加回答
舉報(bào)