LongStream.Builder accept(long t)用于在流的构建阶段将元素插入到元素中。它接受要构建的流的元素。
用法:
void accept(long t)
参数:此方法接受强制参数t,该参数是要输入到流中的元素。
Exceptions:当构建器已经转换到构建状态时,此方法将引发IllegalStateException。这表示流已进入构建阶段,现在不能更改。因此,不再有其他元素可以被接受到流中。
下面是说明accept()方法的示例:
范例1:
// Java code to show the implementation
// of LongStream.Builder accept(long t)
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
LongStream.Builder b = LongStream.builder();
// Inserting elements into the stream
// using LongStream.Builder accept(long t)
b.accept(4L);
b.accept(5L);
b.accept(6L);
b.accept(7L);
// 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:为了说明IllegalStateException
// Java code to show the implementation
// of LongStream.Builder accept(T t)
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
LongStream.Builder b = LongStream.builder();
// using LongStream.Builder accept(T t)
b.accept(4L);
b.accept(5L);
b.accept(6L);
b.accept(7L);
// 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(50L);
}
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
相关用法
- Java LongSummaryStatistics accept(int)用法及代码示例
- Java Stream.Builder accept()用法及代码示例
- Java DoubleStream.Builder accept()用法及代码示例
- Java DoubleSummaryStatistics accept()用法及代码示例
- Java IntSummaryStatistics accept()用法及代码示例
- Java IntStream.Builder accept()用法及代码示例
- Java LongSummaryStatistics accept(long)用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream.Builder accept() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。