📜  Java中的 Stream.concat()

📅  最后修改于: 2022-05-13 01:55:23.987000             🧑  作者: Mango

Java中的 Stream.concat()

Stream.concat() 方法创建一个连接流,其中元素是第一个流的所有元素,然后是第二个流的所有元素。如果两个输入流都是有序的,则结果流是有序的,如果任一输入流是并行的,则结果流是并行的。

句法 :

static  Stream concat(Stream stream1, 
                            Stream stream2)

Where, T is the type of stream elements,
stream1 represents the first stream,
stream2 represents the second stream and
the function returns the concatenation of
the two input streams

对 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 stream1 = Stream.of("Geeks", "for");
        Stream 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 stream1 = Stream.of("Geeks");
        Stream stream2 = Stream.of("GeeksQuiz");
        Stream stream3 = Stream.of("GeeksforGeeks");
        Stream 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 stream1 = Stream.of("Geeks", "for", "GeeksforGeeks");
        Stream 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