guava学习笔记(2)--Optional

Guava 库设计了Optional来解决null问题

1.Optional中三个静态方法:

static <T> Optional<T>absent()
Returns an Optional instance with no contained reference.
static <T> Optional<T>fromNullable(T nullableReference)
If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns absent().
static <T> Optional<T>of(T reference)

@Test
	public void testOptional(){
			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());
				System.out.println("absentOpt value:"+absentOpt.get());
			}
			if(NullableOpt.isPresent()){
				System.out.println("NullableOpt isPresent:"+NullableOpt.isPresent());
				System.out.println("NullableOpt value:"+NullableOpt.get());
			}
			if(NoNullableOpt.isPresent()){
				System.out.println("NoNullableOpt isPresent:"+NoNullableOpt.isPresent());
				System.out.println("NoNullableOpt value:"+NoNullableOpt.get());
			}
	}


测试结果:

possible isPresent:true
possible value:6
NoNullableOpt isPresent:true
NoNullableOpt value:10

2.Optional中实例方法

abstract Set<T>asSet()
Returns an immutable singleton Set whose only element is the contained instance if it is present; an empty immutable Set otherwise.
abstract booleanisPresent()
Returns true if this holder contains a (non-null) instance.

abstract Tget()
Returns the contained instance, which must be present.
abstract Tor(T defaultValue)
Returns the contained instance if it is present; defaultValue otherwise.
abstract TorNull()
Returns the contained instance if it is present; null otherwise.
	@Test
	public void 	testOptional2(){
		Optional<Long> value = mehod();
		if(value.isPresent()){
			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> methodNoNull() {
		return Optional.fromNullable(15L);
	}
	private Optional<Long> mehod() {
		return Optional.fromNullable(null);
	}

测试结果:
返回值:-12
获得返回值 orNull: null
获得返回值 set 的 size : 1
获得返回值: 15
获得返回值 orNull: 15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值