软件正版化情况及网站建设情况,做网站的linux程序代码,连云港网站建设 连云港网站制作,网站后台上传内容前台首页不显示#x1f370; 个人主页:_小白不加班__ #x1f35e;文章有不合理的地方请各位大佬指正。 #x1f349;文章不定期持续更新#xff0c;如果我的文章对你有帮助➡️ 关注#x1f64f;#x1f3fb; 点赞#x1f44d; 收藏⭐️ 文章目录 一、什么是Optional#xff1f;二、… 个人主页:_小白不加班__ 文章有不合理的地方请各位大佬指正。 文章不定期持续更新如果我的文章对你有帮助➡️ 关注 点赞 收藏⭐️ 文章目录 一、什么是Optional二、Optional常用api1.Optional.empty()2.Optional.of()3.Optional.ofNullable4.map5.flatMap6.ifPresent7.get8.orElse9.orElseGet10.orElseThrow11.filter 三、其他问题1. Optional为何不可在 类中使用2.为何避免使用 基础类型的Optional对象 一、什么是Optional
在 Java 8 中Optional 是一个新的容器类它被引入以更好地处理可能为 null 的值。 Optional 的主要目的是提供一种优雅的方式来避免空指针异常NullPointerException
二、Optional常用api 1.Optional.empty()
创建一个空的Optional对象 OptionalCar optCar Optional.empty();
2.Optional.of()
静态工厂方法依据非空值创建对象如果car是一个null这段代码会立即抛出一个NullPointerException OptionalCar optCar Optional.of(car);
3.Optional.ofNullable
创建一个允许null值的Optional 对象如果car是null那么得到的Optional对象就是个空对象。 OptionalCar optCar Optional.ofNullable(car);
4.map
Optional 是一个容器类用于封装可能为 null 的值。不是一个传统的容器如 List、Set、Map只能存储一个值或者不存储任何值即为空Optional对象包含一个非空值那么map方法会根据提供的函数返回一个新的Optional对象该对象包含转换后的值。如果原始Optional对象为空则返回一个空的Optional对象。
// 保险
Data
public class Insurance {private String name;public String getName() {return name;}
}
Test
public void test1() {Insurance insurance new Insurance();insurance.setName(阳光保险);OptionalString name Optional.ofNullable(insurance).map(Insurance::getName);System.out.println(name);//阳光保险
}
Test
public void test2() {Insurance insurance new Insurance();OptionalString name Optional.ofNullable(insurance).map(Insurance::getName);System.out.println(name);//Optional.empty
}5.flatMap
flatMap操作中使用 处理嵌套的 Optional 类型。
// 保险
Data
public class Insurance {private String name;public String getName() {return name;}
}
Test
public void test3() {OptionalInsurance optInsurance Optional.ofNullable(new Insurance());OptionalString name optInsurance.flatMap(insurance - {System.out.println(insurance);//insurancereturn Optional.ofNullable(insurance.getName());});System.out.println(name);/*结果insuranceOptional.empty*/
}6.ifPresent
存在就执行使用该值的方法调用
Test
public void test4() {OptionalInsurance optInsurance Optional.ofNullable(new Insurance());optInsurance.ifPresent(insurance - System.out.println(insurance));
}7.get
如果变量存在它直接返回封装的变量值否则就抛出一个NoSuchElementException异常一般不用单独使用除非确定值存在不然失去了Optional的意义
Test
public void test4() {Insurance insurance new Insurance();insurance.setName(保险);OptionalInsurance optInsurance Optional.ofNullable(insurance);System.out.println(optInsurance.get().getName());//保险
}8.orElse
在 Optional对象不包含值时提供一个默认值。
public String str() {//模拟取值return null;
}
/**
* orElse
*/
Test
public void test5() {String s Optional.ofNullable(str()).orElse(default);System.out.println(s);//default
}9.orElseGet
public String str() {//模拟取值return null;
}
/**** orElseGet*/
Test
public void test6() {SupplierString supplier () - {return default;};String s Optional.ofNullable(str()).orElseGet(supplier);System.out.println(s); // default
}10.orElseThrow
指定Supplier抛出的异常 public String strData() {return null;
}
/*** orElseThrow*/
Test
public void test7() {try {Optional.ofNullable(strData()).orElseThrow(RuntimeException::new);} catch (RuntimeException e) {System.out.println(RuntimeException);}//结果RuntimeException
}11.filter
是否满足谓词满足返回包含该值的Optional对选否则返回Optional.empty
Test
public void test8() {Insurance insurance new Insurance();insurance.setName(A保险);OptionalInsurance optInsurance Optional.ofNullable(insurance);optInsurance.filter((Insurance i) - A保险.equals(i.getName())).ifPresent(System.out::println);
}三、其他问题
1. Optional为何不可在 类中使用
由于Optional类设计时未实现 Serializable接口无法序列化 反例
public class Person {private OptionalCar car;public OptionalCar getCar() { return car; }
}2.为何避免使用 基础类型的Optional对象
除了Optional还提供 OptionalInt、OptionalLong、OptionalDouble 基础类型对象虽然提高了一些性能但是灵活性大大较低基础类型Optional不支持map flatMap filter中间操作 参考《java8 实战》 文章不定期持续更新如果我的文章对你有帮助➡️ 关注 点赞 收藏⭐️