public final class OptionalLong 
 extends Object 
| java.lang.Object | |
| java.util.OptionalLong | |
容器对象,可能包含或不包含long值。 如果值存在, isPresent()将返回true和getAsLong()将返回值。
还提供了其他方法,例如 orElse() (如果值不存在,返回默认值)和 ifPresent() (如果值存在, ifPresent()执行代码块),这些方法取决于是否包含值。
| 公共方法(Public methods) | |
|---|---|
|  static OptionalLong |  empty() 返回一个空的  | 
|  boolean |  equals(Object obj) 指示某个其他对象是否“等于”此OptionalLong。 | 
|  long |  getAsLong() 如果此值存在于此  | 
|  int |  hashCode() 返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。 | 
|  void |  ifPresent(LongConsumer consumer) 如果存在值,指定的消费者接受该值,否则不执行任何操作。 | 
|  boolean |  isPresent() 如果存在值,则返回  | 
|  static OptionalLong |  of(long value) 用指定的值返回  | 
|  long |  orElse(long other) 返回值如果存在,否则返回  | 
|  long |  orElseGet(LongSupplier other) 返回值如果存在,否则调用  | 
|  <X extends Throwable> long |  orElseThrow(Supplier<X> exceptionSupplier) 返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。 | 
|  String |  toString() 返回对象的字符串表示形式。 返回适合调试的此对象的非空字符串表示形式。 | 
| 继承方法(Inherited methods) | |
|---|---|
|  From class  java.lang.Object  | |
OptionalLong empty ()
返回一个空的OptionalLong实例。 这个OptionalLong没有值。
== against instances returned by Option.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().| 返回(Returns) | |
|---|---|
| OptionalLong | an empty OptionalLong. | 
boolean equals (Object obj)
指示某个其他对象是否“等于”此OptionalLong。 另一个对象被认为是相等的,如果:
OptionalLong and; ==. | 参数(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 | 
long getAsLong ()
如果此值为 OptionalLong ,则返回该值,否则返回 NoSuchElementException 。
| 返回(Returns) | |
|---|---|
| long | the value held by this OptionalLong | 
| 抛出异常(Throws) | |
|---|---|
| NoSuchElementException | if there is no value present | 
也可以看看:
int hashCode ()
返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。
| 返回(Returns) | |
|---|---|
| int | hash code value of the present value or 0 if no value is present | 
void ifPresent (LongConsumer consumer)
如果存在值,指定的消费者接受该值,否则不执行任何操作。
| 参数(Parameters) | |
|---|---|
| consumer | LongConsumer: block to be executed if a value is present | 
| 抛出异常(Throws) | |
|---|---|
| NullPointerException | if value is present and consumeris null | 
boolean isPresent ()
如果存在值,则返回 true ,否则 false 。
| 返回(Returns) | |
|---|---|
| boolean | trueif there is a value present, otherwisefalse | 
OptionalLong of (long value)
用指定的值返回 OptionalLong 。
| 参数(Parameters) | |
|---|---|
| value | long: the value to be present | 
| 返回(Returns) | |
|---|---|
| OptionalLong | an OptionalLongwith the value present | 
long orElse (long other)
返回值如果存在,否则返回 other 。
| 参数(Parameters) | |
|---|---|
| other | long: the value to be returned if there is no value present | 
| 返回(Returns) | |
|---|---|
| long | the value, if present, otherwise other | 
long orElseGet (LongSupplier other)
如果存在,则返回值,否则调用 other并返回该调用的结果。
| 参数(Parameters) | |
|---|---|
| other | LongSupplier: aLongSupplierwhose result is returned if no value is present | 
| 返回(Returns) | |
|---|---|
| long | the value if present otherwise the result of other.getAsLong() | 
| 抛出异常(Throws) | |
|---|---|
| NullPointerException | if value is not present and otheris null | 
long orElseThrow (Supplier<X> exceptionSupplier)
返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。
IllegalStateException::new| 参数(Parameters) | |
|---|---|
| exceptionSupplier | Supplier: The supplier which will return the exception to be thrown | 
| 返回(Returns) | |
|---|---|
| long | the present value | 
| 抛出异常(Throws) | |
|---|---|
|  | if there is no value present | 
| NullPointerException | if no value is present and exceptionSupplieris null | 
| Throwable | |
String toString ()
返回对象的字符串表示形式。 通常, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。
类Object的toString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:
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.getClass().getName() + '@' + Integer.toHexString(hashCode())
| 返回(Returns) | |
|---|---|
| String | the string representation of this instance |