Java之Stream.concat()使用示例
下文笔者讲述Stream.concat方法的使用示例分享,如下所示
Stream.concat方法的功能
Stream.concat() 用于合并两个流 Stream.concat()语法 concat(Stream<? extends T> a, Stream<? extends T> b) a:第一流 b:第二流 T:流元素类型例
import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; public class TestClass1 { public static void main(String[] args) { Stream<String> streamOne = Stream.of("Java"); Stream<String> streamTwo = Stream.of("Python"); Stream.concat(streamOne, streamTwo).forEach(val -> System.out.println(val)); } }例2:多个流合并
import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.stream.Stream.*; public class Main { public static void main(String[] args) { Stream<Integer> streamOne = Stream.of(88,89,91); Stream<Integer> streamTwo = Stream.of(66,77,111); Stream<Integer> streamThree = Stream.of(22,434,333); Stream<Integer> streamFour = Stream.of(678,123,801,672); Stream<Integer> res = Stream.concat(streamOne, concat(streamTwo, concat(streamThree, streamFour))); System.out.println( res.collect(Collectors.tolist()) ); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。