📜  Guava-选修课

📅  最后修改于: 2020-11-16 06:50:46             🧑  作者: Mango


可选的是不可变对象,用于包含非空对象。可选对象用于表示缺少值的null。此类具有各种实用程序方法,以方便代码处理可用值或不可用值,而不是检查空值。

类声明

以下是com.google.common.base.Optional 类的声明-

@GwtCompatible(serializable = true)
public abstract class Optional
   extends Object
      implements Serializable

类方法

Sr.No Method & Description
1

static Optionalabsent()

Returns an Optional instance with no contained reference.

2

abstract Set asSet()

Returns an immutable singleton Set whose only element is the contained instance if it is present; an empty immutable Set otherwise.

3

abstract boolean equals(Object object)

Returns true if object is an Optional instance, and either the contained references are equal to each other or both are absent.

4

static OptionalfromNullable(T nullableReference)

If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns absent().

5

abstract Tget()

Returns the contained instance, which must be present.

6

abstract int hashCode()

Returns a hash code for this instance.

7

abstract boolean isPresent()

Returns true if this holder contains a (non-null) instance.

8

static Optionalof(T reference)

Returns an Optional instance containing the given non-null reference.

9

abstract Optional or(Optional secondChoice)

Returns this Optional if it has a value present; secondChoice otherwise.

10

abstract T or(Supplier supplier)

Returns the contained instance if it is present; supplier.get() otherwise.

11

abstract T or(T defaultValue)

Returns the contained instance if it is present; defaultValue otherwise.

12

abstract T orNull()

Returns the contained instance if it is present; null otherwise.

13

static Iterable presentInstances(Iterable> optionals)

Returns the value of each present instance from the supplied optionals, in order, skipping over occurrences of absent().

14

abstract String toString()

Returns a string representation for this instance.

15

abstract Optional transform(Function function)

If the instance is present, it is transformed with the given Function; otherwise, absent() is returned.

继承的方法

此类从以下类继承方法-

  • java.lang.Object

选修课的例子

使用您选择的任何编辑器在C:/> Guava中创建以下Java程序

GuavaTester.java

import com.google.common.base.Optional;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();

      Integer value1 =  null;
      Integer value2 =  new Integer(10);
      
      //Optional.fromNullable - allows passed parameter to be null.
      Optional a = Optional.fromNullable(value1);
      
      //Optional.of - throws NullPointerException if passed parameter is null
      Optional b = Optional.of(value2);        

      System.out.println(guavaTester.sum(a,b));
   }

   public Integer sum(Optional a, Optional b) {
      //Optional.isPresent - checks the value is present or not
      System.out.println("First parameter is present: " + a.isPresent());

      System.out.println("Second parameter is present: " + b.isPresent());

      //Optional.or - returns the value if present otherwise returns
      //the default value passed.
      Integer value1 = a.or(new Integer(0));    

      //Optional.get - gets the value, value should be present
      Integer value2 = b.get();

      return value1 + value2;
   }    
}

验证结果

使用javac编译器编译类,如下所示:

C:\Guava>javac GuavaTester.java

现在运行GuavaTester以查看结果。

C:\Guava>java GuavaTester

查看结果。

First parameter is present: false
Second parameter is present: true
10