[Google Guava]--Optional类

扯淡一下:

      刚刚换了新工作,新的公司,新的同事,很好的伙伴们,都很喜欢;新的业务,新的技术,新的挑战:开启新的旅程,Fighting!

       这阵子一直了解公司业务,看前辈们的代码,发现很多新的技巧,技术,学习好多;那么先从第一个利器:Guava开始说起吧,今天来一张博客,后面持续更新该方面的文章。

简介:

      Optional用于包含非空对象的不可变对象。 Optional对象,用于不存在值表示null。这个类有各种实用的方法,以方便代码来处理为可用或不可用,而不是检查null值。

类声明:

com.google.common.base.Optional<T>类的声明:

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

类方法:

S.N.方法及说明
1static <T> Optional<T> absent()
返回没有包含的参考Optional的实例。
2abstract Set<T> asSet()
返回一个不可变的单集的唯一元素所包含的实例(如果存在);否则为一个空的不可变的集合。
3abstract boolean equals(Object object)
返回true如果对象是一个Optional实例,无论是包含引用彼此相等或两者都不存在。
4static <T> Optional<T> fromNullable(T nullableReference)
如果nullableReference非空,返回一个包含引用Optional实例;否则返回absent()。
5abstract T get()
返回所包含的实例,它必须存在。
6abstract int hashCode()
返回此实例的哈希码。
7abstract boolean isPresent()
返回true,如果这支架包含一个(非空)的实例。
8static <T> Optional<T> of(T reference)
返回包含给定的非空引用Optional实例。
9abstract Optional<T> or(Optional<? extends T> secondChoice)
返回此Optional,如果它有一个值存在; 否则返回secondChoice。
10abstract T or(Supplier<? extends T> supplier)
返回所包含的实例(如果存在); 否则supplier.get()。
11abstract T or(T defaultValue)
返回所包含的实例(如果存在);否则为默认值。
12abstract T orNull()
返回所包含的实例(如果存在);否则返回null。
13static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals)
从提供的optionals返回每个实例的存在的值,从而跳过absent()。
14abstract String toString()
返回此实例的字符串表示。
15abstract <V> Optional<V> transform(Function<? super T,V> function)
如果实例存在,则它被转换给定的功能;否则absent()被返回。

下面直接上代码。

Optional示例:

        Optional<Integer> possible=Optional.of(6);
        Optional<Integer> absentOpt=Optional.absent();
        Optional<Integer> NullableOpt=Optional.fromNullable(null);
        Optional<Integer> NoNullableOpt=Optional.fromNullable(10);
        if(possible.isPresent()){
            System.out.println("possible isPresent:"+possible.isPresent());
            System.out.println("possible value:"+possible.get());
        }
        if(absentOpt.isPresent()){
            System.out.println("absentOpt isPresent:"+absentOpt.isPresent()); ;
        }
        if(NullableOpt.isPresent()){
            System.out.println("fromNullableOpt isPresent:"+NullableOpt.isPresent()); ;
        }
        if(NoNullableOpt.isPresent()){
            System.out.println("NoNullableOpt isPresent:"+NoNullableOpt.isPresent()); ;
        }

输出:

possible isPresent:true
possible value:6
NoNullableOpt isPresent:true
public void testMethodReturn() {
        Optional<Long> value = method();
        if(value.isPresent()==true){
            System.out.println("获得返回值: " + value.get());     
        }else{
                
            System.out.println("获得返回值: " + value.or(-12L));    
        }
        
        System.out.println("获得返回值 orNull: " + value.orNull());
        
        Optional<Long> valueNoNull = methodNoNull();
        if(valueNoNull.isPresent()==true){
            Set<Long> set=valueNoNull.asSet();
            System.out.println("获得返回值 set 的 size : " + set.size());    
            System.out.println("获得返回值: " + valueNoNull.get());     
        }else{
            System.out.println("获得返回值: " + valueNoNull.or(-12L));    
        }
        
        System.out.println("获得返回值 orNull: " + valueNoNull.orNull());
    }

    private Optional<Long> method() {
        return Optional.fromNullable(null);
    }
    private Optional<Long> methodNoNull() {
        return Optional.fromNullable(15L);
    }

输出:

获得返回值: -12
获得返回值 orNull: null
获得返回值 set 的 size : 1
获得返回值: 15
获得返回值 orNull: 15
Optional<Integer> possible = Optional.fromNullable(5); //创建允许null值的Optional 
possible.isPresent(); 
possible.get(); // 5 
possible.or(3)); // 5 
possible.orNull(); // 5 
possible.asSet(); // [5]
final Optional<Integer> possible1 = Optional.fromNullable(100);
Optional<Boolean> a = possible1.transform(new Function<Integer, Boolean>() {

    @Override
    public Boolean apply(Integer input) {
        return 100 == input ? Boolean.TRUE : Boolean.FALSE;
    }
});
a.get();// true
List<Optional<Integer>> list = new ArrayList<Optional<Integer>>();
        for (int index = 10; index > 0; --index) {
            Integer t;
            if (0 == index % 2) {
                t = index;
            } else {
                t = null;
            }
            list.add(Optional.<Integer>fromNullable(t));
        }
        Iterable<Integer> it = Optional.presentInstances(list);
        Iterator iter= it.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }

输出:

//10
//8
//6
//4
//2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值