Stream.Builder 的 build() 函数在 Java 中
Stream.Builder 类中的 build() 函数构建了流,将此构建器转变为构建状态。语法如下:-
Stream<T> build()
接下来是一个示例,来说明如何实现 Stream.Builder 类的 build() 函数:
示例
import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream.Builder<String> builder = Stream.builder(); builder.add("Production"); builder.add("Marketing"); builder.add("Finance"); builder.add("Sales"); builder.add("Operations"); Stream<String> stream = builder.build(); stream.forEach(System.out::println); } }
输出
Production Marketing Finance Sales Operations
示例
让我们看另一个 build() 函数的示例,其中我们使用 accept() 函数向流添加元素:-
import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream.Builder<String> builder = Stream.builder(); builder.accept("k"); builder.accept("l"); builder.accept("m"); builder.accept("n"); builder.accept("o"); builder.accept("p"); builder.accept("q"); builder.accept("r"); builder.accept("s"); builder.accept("t"); Stream<String> stream = builder.build(); stream.forEach(System.out::println); } }
输出
K l m n o p q r s t
广告