Java Stream.concat()
Stream.concat()方法创建一个连接的流,其中的元素是第一个流的所有元素,然后是第二个流的所有元素。如果两个输入流都是有序的,那么生成的流就是有序的,如果其中一个输入流是平行的,那么生成的流就是平行的。
语法:
static <T> Stream<T> concat(Stream<? extends T > stream1,
Stream<? extends T > stream2)
其中,T是流元素的类型。
stream1代表第一个流。
stream2代表第二个流,并且
该函数返回两个输入流的连接值
两个输入流的连接
对Stream.concat(stream1, stream2)的调用可以被认为是形成了一棵二进制树。所有输入流的串联在根部。单独的输入流在叶子上。下面给出了一些树的例子,最多有四个输入流a、b、c和d。
对于两个流a和b,树看起来像:
对于三个流a、b和c,树看起来像:
对于四个流a、b、c和d,树看起来像:
每增加一个输入流就增加一层树的深度和一层指示,以达到所有其他的流。
注意: Stream.concat()方法返回的元素是有序的。例如,下面两行返回相同的结果。
Stream.concat(Stream.concat(stream1, stream2), stream3);
Stream.concat(stream1, Stream.concat(stream2, stream3));
但下面两个人的结果是不同的。
Stream.concat(Stream.concat(stream1, stream2), stream3);
Stream.concat(Stream.concat(stream2, stream1), stream3);
下面是一些例子,以便更好地理解函数的实现。
例1 :
// Implementation of Stream.concat()
// method in Java 8 with 2 Streams
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two Streams
Stream<String> stream1 = Stream.of("Geeks", "for");
Stream<String> stream2 = Stream.of("GeeksQuiz", "GeeksforGeeks");
// concatenating both the Streams
// with Stream.concat() function
// and displaying the result
Stream.concat(stream1, stream2)
.forEach(element -> System.out.println(element));
}
}
输出:
Geeks
for
GeeksQuiz
GeeksforGeeks
例2 :
// Implementation of Stream.concat()
// method in Java 8 with more than
// two Streams
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating more than two Streams
Stream<String> stream1 = Stream.of("Geeks");
Stream<String> stream2 = Stream.of("GeeksQuiz");
Stream<String> stream3 = Stream.of("GeeksforGeeks");
Stream<String> stream4 = Stream.of("GFG");
// concatenating all the Streams
// with Stream.concat() function
// and displaying the result
Stream.concat(Stream.concat(Stream.concat(stream1,
stream2), stream3), stream4)
.forEach(element -> System.out.println(element));
}
}
输出:
Geeks
GeeksQuiz
GeeksforGeeks
GFG
例3 :
// Implementation of Stream.concat()
// method in Java 8 with DoubleStream
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two Streams
DoubleStream Stream1 = DoubleStream.of(1520, 1620);
DoubleStream Stream2 = DoubleStream.of(1720, 1820);
// concatenating both the Streams and
// displaying the result
DoubleStream.concat(Stream1, Stream2)
.forEach(element -> System.out.println(element));
}
}
输出:
1520.0
1620.0
1720.0
1820.0
例4 :
// Implementation of Stream.concat()
// method in Java 8 and removing
// the duplicates
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two Streams
Stream<String> stream1 = Stream.of("Geeks", "for", "GeeksforGeeks");
Stream<String> stream2 = Stream.of("GeeksQuiz", "GeeksforGeeks", "for");
// concatenating both the Streams
// with Stream.concat() function
// and displaying the result after
// removing the duplicates
Stream.concat(stream1, stream2).distinct().forEach(element -> System.out.println(element));
}
}
输出:
Geeks
for
GeeksforGeeks
GeeksQuiz
例5 :
// Implementation of Stream.concat()
// method in Java 8 with LongStream
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating two Streams
LongStream Stream1 = LongStream.of(1520, 1620);
LongStream Stream2 = LongStream.of(1720, 1820);
// concatenating both the Streams and
// displaying the result
LongStream.concat(Stream1, Stream2)
.forEach(element -> System.out.println(element));
}
}
输出:
1520
1620
1720
1820