Optional
5.1 提供方法:
1). of(),提供转换成Optional对象,不能处理null,如果值是null,直接抛出NullPointerException
2). ofNullable(),提供转换成Optional对象,可以处理null
3). isPresent(),存在返回true,不存在返回false,null返回false,””返回true
4). ifPresent(),判断是否存在,存在则返回,不存在返回Optional.empty。value为lambada表达式
5). orElse(T other),存在则返回调用对象,不存在则返回other对象
6). orElseGet()
上面的方法都是最终操作,要么返回boolean,要么返回T本身
7). map(),value为lambada表达式
这个方法是中间操作,返回Optional对象
5.2 实例:
Optional<String> optional = Optional.of("tom");
System.out.println("present...."+optional.isPresent());
System.out.println("orElse...." + optional.orElse("jack"));
optional.ifPresent((s)-> System.out.println(s.charAt(0)));
Optional.of(null);// java.lang.NullPointerException
optional = Optional.ofNullable(null);
System.out.println("present...."+optional.isPresent());//false
System.out.println("orElse...." + optional.orElse("jack"));//jack
optional.ifPresent((s)-> System.out.println(s.charAt(0)));//空白
optional = Optional.ofNullable("");
System.out.println("present...."+optional.isPresent()); //true
System.out.println("orElse...." + optional.orElse("jack"));//空白
optional.ifPresent((s)-> System.out.println(s.charAt(0)));//java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Person person = new Person();
System.out.println("of.........." + Optional.of(person));// Optional[Person{id=0, name='null'}]
Optional<Person> personOptional = Optional.ofNullable(person); System.out.println("ofNullable........."+personOptional); // Optional[Person{id=0, name='null'}]
System.out.println("isPresent............"+personOptional.isPresent());//true
personOptional.ifPresent(System.out::println);// Person{id=0, name='null'}
System.out.println("orElse ......." + personOptional.orElse(new Person(1, "yanweijie")).toString());//orElse .......Person{id=0, name='null'}
person = null;
personOptional = Optional.ofNullable(person);
System.out.println("ofNullable.null......."+personOptional);// ofNullable.null.......Optional.empty
System.out.println("isPresent............"+personOptional.isPresent());
personOptional.ifPresent(System.out::println);//没有输出
System.out.println("orElse........." + personOptional.orElse(new Person(1, "yanweijie")).toString());//orElse.........Person{id=1, name='yanweijie'}
//一行代码取代了无数的if、else
System.out.println(Optional.ofNullable(new Person(2,"baiweijing")).map(p -> p.getName())
.map(name -> name.toUpperCase())
.orElse(null));
以下代码是我改造的,项目中实际用到的:
return Optional.ofNullable(obj).map((o)->{
return JSONArray.parseArray(o.toString(), GiftWithDrawConfig.class);
}).orElseGet(()->{
List<GiftWithDrawConfig> giftWithDrawConfigs = giftWithDrawConfigDao.findGiftWithDrawConfigs();
if(!CollectionUtils.isEmpty(giftWithDrawConfigs)) {
memcachedProvider.set(activeProfile, "GIFT_WITHDRAW_CONFIG", JSONObject.toJSONString(giftWithDrawConfigs), CacheUtils.MC_DAY_7);
}
return giftWithDrawConfigs;
});
Java8之前的判断:
/*if(null != user) {
kfInfo.setIsOnline(user.getIsOnline());
}*/
//Optional.ofNullable(user).ifPresent((u)->{kfInfo.setIsOnline(u.getIsOnline());});
kfInfo.setIsOnline(Optional.ofNullable(user).map(u->u.getIsOnline()).orElse(0));