目录
Optional
可以避免空指针异常<br/>
创建对象
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
}
<br/>
安全消费值
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));
}
<br/>
安全获取值
不推荐使用get()
ofElseGet
获取数据并且设置为空时的默认值,为null就返回默认值
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
Author author1 = authorOptional.orElseGet(() -> new Author());
System.out.println(author1.getName());
}
<br/>
ofElseThrow
为空就根据传入的参数进行异常抛出
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
try {
Author author1 = authorOptional.orElseThrow((Supplier<Throwable>) () -> {
throw new RuntimeException("为空");
});
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
<br/>
过滤
不符合过滤条件返回的是新的Optional对象,其值为null
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> author1 = Optional.ofNullable(author);
author1.filter(author2 -> author2.getAge() > 18)
.ifPresent(author22 -> System.out.println(author22.getAge()));
}
<br/>
判断
进行是否存在数据的判断,如果为空返回false,为true就不为空
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
if (authorOptional.isPresent()) {
System.out.println(authorOptional.get());
}
}
<br/>
数据转换
public static void main(String[] args) {
Author author = getAuthors().get(0);
Optional<Author> authorOptional = Optional.ofNullable(author);
authorOptional.map(au ->au.getBookList())
.ifPresent(books -> System.out.println(books));
}
<br/><br/>
函数式接口
只有一个抽象方法的接口称为函数接口
常用默认方法
and 相当于 &&找出年龄大于17并且长度大于1的作家
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge() > 17
}
}.and(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getName() .length() > 1
}
})).forEach(author -> System.out.println(author.getAge()));
}
or 相当于||打印年龄小于17的
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge() > 17;
}
}.or(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getName() .length() > 1;
}
})).forEach(author -> System.out.println(author.getAge()));
}
negate 相当于 !
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge() > 17;
}
}.negate())
.forEach(author -> System.out.println(author.getAge()));
}
基本数据类型优化
数据量较大时,节约自动拆箱装箱的时间优化前
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getAge())
}
优化后
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.mapToInt(author -> author.getAge())
}