Guava – Optional类

Guava – Optional 类

Optional是一个不可变的对象,用于包含一个非空对象。可选对象被用来表示没有数值的空值。这个类有各种实用方法,以方便代码处理可用或不可用的值,而不是检查空值.

Optional 类定义

以下是com.google.common.base.Optional <T>类的声明 −

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

Optional 类函数

序号 函数和描述
1 static <T> Optional<T> absent()
返回一个没有包含引用的Optional实例.
2 abstract Set<T> asSet()
返回一个不可变的单子集合,如果该集合存在,其唯一的元素就是所包含的实例;否则就是一个空的不可变的集合.
3 abstract boolean equals(Object object)
如果对象是一个Optional实例,并且所包含的引用彼此相等或两者都不存在,则返回true.
4 static <T> Optional<T> fromNullable(T nullableReference)
如果nullableReference不是空的,返回一个包含该引用的Optional实例;否则返回absent().
5 abstract T get()
返回所包含的实例,该实例必须存在.
6 abstract int hashCode()
返回该实例的哈希代码.
7 abstract boolean isPresent()
如果这个持有人包含一个(非空的)实例,则返回真.
8 static <T> Optional<T> of(T reference)
返回一个包含给定非空引用的Optional实例.
9 abstract Optional<T> or(Optional<? extends T> secondChoice)
如果有一个值存在,返回这个Optional;否则返回secondChoice.
10 abstract T or(Supplier<? extends T> supplier)
如果包含的实例存在,则返回该实例;否则返回supplier.get().
11 abstract T or(T defaultValue)
如果所包含的实例存在,则返回该实例;否则返回默认值.
12 abstract T orNull()
如果包含的实例存在,则返回该实例;否则为空.
13 static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals)
从提供的选项中依次返回每个present实例的值,跳过absence()的出现次数.
14 abstract String toString()
返回这个实例的字符串表示.
15 abstract <V> Optional<V> transform(Function<? super T,V> function)
如果实例存在,则用给定的Function对其进行转换;否则,返回absent().

Optional 类继承

该类继承了以下类的方法 −

  • java.lang.Object

Optional 类示例

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<Integer> a = Optional.fromNullable(value1);

      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);        

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

   public Integer sum(Optional<Integer> a, Optional<Integer> 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

赞(2)

评论 抢沙发

评论前必须登录!

 

Guava 教程
Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
Guava Ints类
Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
Guava Chars类
Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
Guava LongMath类
Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
Guava Longs类
Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
Guava Booleans类
Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
Guava Shorts类
Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
Guava IntMath类
Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例