LongStream concat()方法創建一個串聯的流,其中的元素是第一個流的所有元素,然後是第二個流的所有元素。如果兩個輸入流都是有序的,則結果流是有序的;如果兩個輸入流中的任何一個是並行的,則結果流是並行的。
用法:
static LongStream concat(LongStream a, LongStream b)
參數:
- LongStream : 一係列原始long值元素。
- a : 代表第一流。
- b : 代表第二個流。
返回值:該函數返回兩個輸入LongStream的串聯。
可以將對LongStream.concat(LongStream a,LongStream b)的調用視為形成二叉樹。所有輸入流的串聯都在根。各個輸入流位於葉子處。以下是3個LongStream a,b和c的示例。
每個附加的輸入流為樹增加了一層深度,並為所有其他流增加了一層間接。
注意:LongStream.concat()方法返回的元素是有序的。例如,以下兩行返回相同的結果:
LongStream.concat(LongStream.concat(stream1, stream2), stream3); LongStream.concat(stream1, LongStream.concat(stream2, stream3));
但是以下兩個結果是不同的。
LongStream.concat(LongStream.concat(stream1, stream2), stream3); LongStream.concat(LongStream.concat(stream2, stream1), stream3);
示例1:
// Implementation of LongStream concat()
// method in Java 8 with 2 LongStreams
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two LongStreams
LongStream stream1 = LongStream.of(2L, 4L, 6L);
LongStream stream2 = LongStream.of(1L, 3L, 5L);
// concatenating both the Streams
// with LongStream concat() function
// and displaying the result
LongStream.concat(stream1, stream2)
.forEach(element -> System.out.println(element));
}
}
輸出:
2 4 6 1 3 5
示例2:
// Implementation of LongStream concat()
// method in Java 8 with 2 LongStreams
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two LongStreams
LongStream stream1 = LongStream.of(2L, 4L, 6L);
LongStream stream2 = LongStream.of(1L, 2L, 4L);
// concatenating both the Streams
// with LongStream concat() function
// and displaying distinct elements
// in the concatenated LongStream
LongStream.concat(stream1, stream2).distinct().
forEach(element -> System.out.println(element));
}
}
輸出:
2 4 6 1
相關用法
- Java LongStream of()用法及代碼示例
- Java LongStream sum()用法及代碼示例
- Java Stream.concat()用法及代碼示例
- Java IntStream concat()用法及代碼示例
- Java DoubleStream concat()用法及代碼示例
- Java LongStream min()用法及代碼示例
- Java LongStream parallel()用法及代碼示例
- Java LongStream sorted()用法及代碼示例
- Java LongStream max()用法及代碼示例
- Java LongStream mapToDouble()用法及代碼示例
- Java LongStream summaryStatistics()用法及代碼示例
- Java LongStream builder()用法及代碼示例
- Java LongStream boxed()用法及代碼示例
- Java LongStream asDoubleStream()用法及代碼示例
- Java LongStream limit()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream concat() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。