当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Stream.concat()用法及代码示例


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

用法:

static <T> Stream<T> concat(Stream<? extends T> stream1, 
                            Stream<? extends T> 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<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


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream.concat() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。