IntStream。建造商验收(int t) 用于在流的构建阶段将图元插入到图元中。它接受正在构建的流的元素。
null
语法:
void accept(int t)
参数: 此方法接受一个强制参数 T 这是要输入到流中的元素。
例外情况: 这个方法抛出 非法国家例外 当构建器已转换为已构建状态时。这意味着流已经进入了构建阶段,现在不能更改。因此 流中不能接受更多元素。
下面是示例来说明accept()方法:
例1:
// Java code to show the implementation // of IntStream.Builder accept(int t) import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Declaring an empty Stream IntStream.Builder b = IntStream.builder(); // Inserting elements into the stream // using IntStream.Builder accept(int t) b.accept( 4 ); b.accept( 5 ); b.accept( 6 ); b.accept( 7 ); // Creating the Stream // The stream has now entered the built phase // printing the elements System.out.println( "Stream successfully built" ); b.build().forEach(System.out::println); } } |
输出:
Stream successfully built 4 5 6 7
例2: 来说明非法的例外情况
// Java code to show the implementation // of IntStream.Builder accept(int t) import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Declaring an empty Stream IntStream.Builder b = IntStream.builder(); // using IntStream.Builder accept(int t) b.accept( 4 ); b.accept( 5 ); b.accept( 6 ); b.accept( 7 ); // Creating the Stream // The stream has now entered the built phase // printing the elements System.out.println( "Stream successfully built" ); b.build().forEach(System.out::println); // Trying to accept 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 { b.accept( 50 ); } catch (Exception e) { System.out.println( "Exception thrown " + "when now accepting element into the stream: " + e); } } } |
输出:
Stream successfully built 4 5 6 7 Exception thrown when now accepting element into the stream: java.lang.IllegalStateException
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END