Java中的 Stream.Builder accept() 方法
Stream.Builder accept(T t)用于在流的构建阶段将元素插入到元素中。它将一个元素添加到正在构建的流中。
句法:
void accept(T t)
参数:此方法接受一个强制参数t ,它是要输入到流中的元素。
异常:此方法抛出IllegalStateException:当构建器已经转换到构建状态时。这意味着流已进入构建阶段,现在不能更改。因此,不能将更多元素添加到流中。
以下是说明 accept() 方法的示例:
示例 1:
// Java code to show the implementation
// of Stream.Builder accept(T t)
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
Stream.Builder str_b = Stream.builder();
// Inserting elements into the stream
// using Stream.Builder accept(T t)
str_b.accept("Geeks");
str_b.accept("for");
str_b.accept("GeeksforGeeks");
str_b.accept("Data Structures");
str_b.accept("Geeks Classes");
// Creating the String Stream
// The stream has now entered the built phase
Stream s = str_b.build();
// printing the elements
System.out.println("Stream successfully built");
s.forEach(System.out::println);
}
}
输出:
Stream successfully built
Geeks
for
GeeksforGeeks
Data Structures
Geeks Classes
示例 2:说明 IllegalStateException
// Java code to show the implementation
// of Stream.Builder accept(T t)
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
Stream.Builder str_b = Stream.builder();
// using Stream.Builder accept(T t)
str_b.accept("5");
str_b.accept("6");
str_b.accept("7");
str_b.accept("8");
str_b.accept("9");
// Creating the String Stream
// The stream has now entered the built phase
Stream s = str_b.build();
// printing the elements
System.out.println("Stream successfully built");
s.forEach(System.out::println);
// Trying to add another element into the stream
// Since the Stream is in built phase
// This operation is not possible now
// Hence accept() will throw exception now
try {
str_b.accept("50");
}
catch (Exception e) {
System.out.println("Exception thrown "
+ "when now adding element into the stream: "
+ e);
}
}
}
输出:
Stream successfully built
5
6
7
8
9
Exception thrown when now adding element into the stream: java.lang.IllegalStateException