Most visited

Recently visited

Added in API level 24

OptionalDouble

public final class OptionalDouble
extends Object

java.lang.Object
   ↳ java.util.OptionalDouble


一个容器对象,其中可能包含或不包含double值。 如果存在值,则isPresent()将返回true并且getAsDouble()将返回该值。

还提供了依赖于包含值是否存在的其他方法,如 orElse() (如果值不存在,返回默认值)和 ifPresent() (如果值存在, ifPresent()执行一个代码块)。

Summary

Public methods

static OptionalDouble empty()

返回一个空的 OptionalDouble实例。

boolean equals(Object obj)

指示某个其他对象是否“等于”此OptionalDouble。

double getAsDouble()

如果此值为 OptionalDouble ,则返回该值,否则返回 NoSuchElementException

int hashCode()

返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。

void ifPresent(DoubleConsumer consumer)

如果存在值,指定的消费者接受该值,否则不执行任何操作。

boolean isPresent()

如果存在值,则返回 true ,否则 false

static OptionalDouble of(double value)

用指定的值返回 OptionalDouble

double orElse(double other)

返回值如果存在,否则返回 other

double orElseGet(DoubleSupplier other)

返回值如果存在,否则调用 other并返回该调用的结果。

<X extends Throwable> double orElseThrow(Supplier<X> exceptionSupplier)

返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。

String toString()

返回对象的字符串表示形式。 返回适合调试的此对象的非空字符串表示形式。

Inherited methods

From class java.lang.Object

Public methods

empty

Added in API level 24
OptionalDouble empty ()

返回一个空的OptionalDouble实例。 这个OptionalDouble没有值。

API Note:
  • Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Option.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().
Returns
OptionalDouble an empty OptionalDouble.

equals

Added in API level 24
boolean equals (Object obj)

指示某个其他对象是否“等于”此OptionalDouble。 另一个对象被认为是相等的,如果:

  • it is also an OptionalDouble and;
  • both instances have no value present or;
  • the present values are "equal to" each other via Double.compare() == 0.

Parameters
obj Object: an object to be tested for equality
Returns
boolean {code true} if the other object is "equal to" this object otherwise false

getAsDouble

Added in API level 24
double getAsDouble ()

如果此值存在于此 OptionalDouble ,则返回该值,否则将抛出 NoSuchElementException

Returns
double the value held by this OptionalDouble
Throws
NoSuchElementException if there is no value present

也可以看看:

hashCode

Added in API level 24
int hashCode ()

返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。

Returns
int hash code value of the present value or 0 if no value is present

ifPresent

Added in API level 24
void ifPresent (DoubleConsumer consumer)

如果存在值,指定的消费者接受该值,否则不执行任何操作。

Parameters
consumer DoubleConsumer: block to be executed if a value is present
Throws
NullPointerException if value is present and consumer is null

isPresent

Added in API level 24
boolean isPresent ()

如果存在值,则返回 true ,否则 false

Returns
boolean true if there is a value present, otherwise false

of

Added in API level 24
OptionalDouble of (double value)

用指定的值返回 OptionalDouble

Parameters
value double: the value to be present
Returns
OptionalDouble an OptionalDouble with the value present

orElse

Added in API level 24
double orElse (double other)

返回值如果存在,否则返回 other

Parameters
other double: the value to be returned if there is no value present
Returns
double the value, if present, otherwise other

orElseGet

Added in API level 24
double orElseGet (DoubleSupplier other)

返回值如果存在,否则调用 other并返回该调用的结果。

Parameters
other DoubleSupplier: a DoubleSupplier whose result is returned if no value is present
Returns
double the value if present otherwise the result of other.getAsDouble()
Throws
NullPointerException if value is not present and other is null

orElseThrow

Added in API level 24
double orElseThrow (Supplier<X> exceptionSupplier)

返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。

API Note:
  • A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new
Parameters
exceptionSupplier Supplier: The supplier which will return the exception to be thrown
Returns
double the present value
Throws
if there is no value present
NullPointerException if no value is present and exceptionSupplier is null
Throwable

toString

Added in API level 24
String toString ()

返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Returns a non-empty string representation of this object suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.

实现要求:
  • If a value is present the result must include its string representation in the result. Empty and present instances must be unambiguously differentiable.
Returns
String the string representation of this instance

Hooray!