DoubleStream.Builder accept(double t)用於在流的構建階段將元素插入到元素中。它接受要構建的流的元素。
用法:
void accept(double t)
參數:此方法接受強製參數t,該參數是要輸入到流中的元素。
Exceptions:當構建器已經轉換到構建狀態時,此方法將引發IllegalStateException。這表示流已進入構建階段,現在不能更改。因此,不再有其他元素可以被接受到流中。
下麵是說明accept()方法的示例:
範例1:
// Java code to show the implementation
// of DoubleStream.Builder accept(double t)
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
DoubleStream.Builder b = DoubleStream.builder();
// Inserting elements into the stream
// using DoubleStream.Builder accept(double t)
b.accept(45.54);
b.accept(415.2541);
b.accept(12.21);
b.accept(778);
// 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 45.54 415.2541 12.21 778.0
範例2:為了說明IllegalStateException
// Java code to show the implementation
// of DoubleStream.Builder accept(double t)
import java.util.stream.DoubleStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Declaring an empty Stream
DoubleStream.Builder b = DoubleStream.builder();
// using DoubleStream.Builder accept(double t)
b.accept(45.54);
b.accept(415.2541);
b.accept(12.21);
b.accept(778);
// 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(75.1452);
}
catch (Exception e) {
System.out.println("Exception thrown "
+ "when now accepting element into the stream:"
+ e);
}
}
}
輸出:
Stream successfully built 45.54 415.2541 12.21 778.0 Exception thrown when now accepting element into the stream:java.lang.IllegalStateException
相關用法
- Java LongSummaryStatistics accept(int)用法及代碼示例
- Java Stream.Builder accept()用法及代碼示例
- Java DoubleSummaryStatistics accept()用法及代碼示例
- Java IntSummaryStatistics accept()用法及代碼示例
- Java IntStream.Builder accept()用法及代碼示例
- Java LongStream.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大神的英文原創作品 DoubleStream.Builder accept() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。