Stream.Builder类的build()方法将构建流,并将此构建器转换为已构建状态。语法如下-Stream build()
以下是实现Stream.Builder类的build()方法的示例-
示例import java.util.stream.Stream;
public class Demo {
public static void main(String[] args) {
Stream.Builder builder = Stream.builder();
builder.add("Production");
builder.add("Marketing");
builder.add("Finance");
builder.add("Sales");
builder.add("Operations");
Stream 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 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 stream = builder.build();
stream.forEach(System.out::println);
}
}
输出结果K
l
m
n
o
p
q
r
s
t