當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。