java8的Stream使用的是函数式编程模式,如同它的名字一样,它可以被用来对集合或者数组进行链状流式操作。可以方便的让我们的对集合或者数组操作;
一定需要有终结操作
List<Author> authors = getAuthors();
Stream<Author> stream = authors.stream();
Integer [] arr ={1,2,3,4};
Stream<Integer> stream = Arrays.stream(arr);
Stream<Integer> stream1 = Stream.of(arr);
Map<String, Integer> map = new HashMap<>();
map.put("a",11);
map.put("b",12);
map.put("c",13);
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Stream<Map.Entry<String, Integer>> stream = entries.stream();
List<Author> authors = getAuthors();
//打印年龄小于18的名字,并且去重
authors.stream()
//筛选
.filter(author -> author.getAge()<18)
//遍历
.forEach(author -> System.out.println(author.getName()));
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getAge())
.map(age->age+10)
.forEach(age-> System.out.println(age));
List<Author> authors = getAuthors();
authors.stream()
//去重
.distinct()
//遍历
.forEach(author -> System.out.println(author.getName()));
List<Author> authors = getAuthors();
authors.stream()
//去重
.distinct()
.sorted((o1, o2) -> o1.getAge()-o2.getAge())
//遍历
.forEach(author -> System.out.println(author.getAge()));
List<Author> authors = getAuthors();
authors.stream()
//去重
.distinct()
//排序
.sorted((o1, o2) -> o1.getAge()-o2.getAge())
//限制长度
.limit(2)
//遍历
.forEach(author -> System.out.println(author.getAge()));
List<Author> authors = getAuthors();
authors.stream()
//去重
.distinct()
//排序
.sorted((o1, o2) -> o1.getAge()-o2.getAge())
//跳过第一元素
.skip(1)
//遍历
.forEach(author -> System.out.println(author.getAge()));
List<Author> authors = getAuthors();
authors.stream()
.flatMap(author -> author.getBooks().stream())
.forEach(booK -> System.out.println(booK));
}
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getName())
.collect(Collectors.toList());
List<Author> authors = getAuthors();
authors.stream()
.collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));
//将年龄相加
List<Author> authors = getAuthors();
Integer reduce = authors.stream()
.distinct()
.map(author -> author.getAge())
.reduce(0, (integer, integer2) -> integer + integer2);
System.out.println(reduce);
我们在编写代码时经常会出现空指针异常,所以在很多情况下我们需要做出各种非空的判断。
在jdk8中引入了Optional,可以极大程度的避免空指针异常。
1.创建对象
Optional就好像是包装类,可以把我们的具体数据封装Optional对象内部,然后使用Optional中封装好的方法操作封装进去的数据可以非常优雅的避免空指针异常。
Author author = getAuthor();
Optional<Author> author1 = Optional.ofNullable(author);
author1.ifPresent(author2 -> System.out.println(author2.getName()));
或者直接封装到方法上,统一格式
private static Optional<Author> getAuthor(){
Author author = new Author(1L, "a", 13, "dadadadea", null);
return Optional.ofNullable(author);
}
public static void main(String[] args) {
Optional<Author> author = getAuthor();
author.ifPresent(author1 -> System.out.println(author1.getName()));
}
2.安全消费值
我们可以使用ifPresent方法,这个方法会判断其内封装的数据是否为空,不为空时才会执行具体的消费代码,这样更安全。
3.安全的获取值
获取到Optional返回的值,不推荐使用get方法
推荐使用orElseGet或orElseThrow
Optional<Author> author = getAuthor();
//有值就返回,没有值就返回默认值
Author author1 = author.orElseGet(() -> new Author());
System.out.println(author1.getName());
Optional<Author> author = getAuthor();
//有值就返回,没有值就抛出自定义的异常
try {
author.orElseThrow((Supplier<Throwable>) () -> new RuntimeException("数据异常"));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
4.过滤
可以使用filter方法对数据进行过滤。如果原本是有数据,但不符合判断,就会变成一个无数据的optional对象。
Optional<Author> author = getAuthor();
author.filter(author1 -> author1.getAge() > 10).ifPresent(author1 -> System.out.println(author1.getName()));
5.判断
我们可以使用ifPresent方法进行判断,这个方法会判断其内封装的数据是否为空,不为空时,返回true。
Optional<Author> author = getAuthor();
if (author.isPresent()){
System.out.println(author.get().getName());
}
6.数据转换
数据转换,我们可以使用map,转换后也是Optional包装好的,保证了我们使用更安全。
Optional<Author> author = getAuthor();
author.map(author1 -> author1.getBooks())
.ifPresent(booKS -> System.out.println(booKS));
定义:只有一个抽象方法的接口称之为函数接口。
常见函数接口
我们在使用lambda时,如果方法体中只有一个方法的调用的话,我们可以用方法引用进一步简化代码。
格式:类名或这对象名::方法名
List<Author> authors = getAuthors();
authors.stream()
.map(Author::getName)
.collect(Collectors.toList());