Java新特性(二) Stream与Optional详解

news2024/9/22 23:26:56

Java8新特性(二) Stream与Optional详解

一. Stream流

1. Stream概述

1.1 基本概念

Stream(java.util.stream) 是Java 8中新增的一种抽象流式接口,主要用于配合Lambda表达式提高批量数据的计算和处理效率。Stream不是一种数据结构,它是来自数据源的一种元素序列组织视图,并支持一系列聚合操作,比如过滤、排序、映射、遍历等;其中,数据源可以是一个数组、集合或I/O channel等。

Stream的主要目的在于计算,它以一种声明性方式处理数据集合与数据,是函数式编程模式的重要体现。关于Stream流,可以把它理解为一种管道式的操作集,它允许将一系列计算过程进行链式封装,并像流水线一样对数据源进行处理,最后生成并返回结果集合。

1.2 操作流程

(1)流的创建: 一个数据源(如集合、数组等),用于获取一个流;

(2)中间操作: 一个中间操作链(包含多个中间操作),每个中间操作都会返回一个新的流,用于对数据源的数据进行处理;

(3)终止操作: 一个终止操作,用于执行中间操作链并产生最终结果,注意终止操作后该Stream就不能再使用了;

1.3 基本特性

  • 惰性求值: 流上的中间操作不会立即执行(仅创建视图),只有在遇到终止操作时才会触发计算;
  • 非数据结构: 不会保存数据,也不会存储元素,只保存操作;
  • 不改变源数据: 不会改变源数据,计算结果会保存到新的流中,但对于对象引用来说可以修改其属性值;

2. 流的创建

2.1 从数组创建

  • Stream.of: 调用Stream类的静态方法 of(),通过显示值创建一个流;
  • Arrays.stream: 调用Arrays 的静态方法 stream(),来从数组中创建一个流;
Integer[] arr = {1,2,3,4,5};
Stream<Integer> stream_arr = Arrays.stream(arr);
// Stream<Integer> stream_of = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> stream_of = Stream.of(arr);

2.2 从单列集合

单列集合如List、Set,Java8 中的 Collection 接口被扩展,提供了获取集合流的方法 Collection.stream()

List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Stream<String> stream = names.stream();

2.3 从双列集合

双列集合如Map,一般转换成单列集合后再创建流

Map<String,Integer> ids = new HashMap<>();
ids.put("小新",19);
ids.put("黑子",17);
ids.put("哆啦",16);
Stream<Map.Entry<String, Integer>> stream = ids.entrySet().stream();

3. 数据准备

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode//用于后期的去重使用
public class Book {
    //id
    private Long id;
    //书名
    private String name;
    //分类
    private String category;
    //评分
    private Integer score;
    //简介
    private String intro;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode//用于后期的去重使用
public class Author {
    //id
    private Long id;
    //姓名
    private String name;
    //年龄
    private Integer age;
    //国家
    private String country;
    //作品
    private List<Book> books;
}
       private static List<Author> getAuthors() {
        //数据初始化
        Author author1 = new Author(1L, "鲁迅", 55, "中国", null);
        Author author2 = new Author(2L, "维克多.雨果", 83, "法国", null);
        Author author3 = new Author(3L, "安东.巴甫洛维奇.契诃夫", 44, "俄国", null);
        Author author4 = new Author(3L, "安东.巴甫洛维奇.契诃夫", 44, "俄国", null);
        //书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();
        books1.add(new Book(1L, "朝花夕拾", "散文,回忆", 82, "这部作品收录了鲁迅于1926年创作的10篇回忆性散文"));
        books1.add(new Book(2L, "孔乙己", "短篇小说,讽刺", 99, "深刻揭示了当时社会的黑暗面"));
        books2.add(new Book(3L, "巴黎圣母院", "长篇小说,浪漫主义", 75, "通过离奇和对比手法,描述了15世纪法国的故事"));
        books2.add(new Book(3L, "巴黎圣母院", "长篇小说,浪漫主义", 75, "通过离奇和对比手法,描述了15世纪法国的故事"));
        books2.add(new Book(4L, "悲惨世界", "现实,长篇小说", 86, "这部小说跨越了拿破仑战争及其后的十几年历史"));
        books3.add(new Book(5L, "变色龙", "短篇小说,讽刺", 86, "巧妙地讽刺了沙皇专制制度下封建卫道士的卑躬屈膝和虚伪"));
        books3.add(new Book(6L, "装在套子里的人", "短篇小说", 100, "深刻地揭示了专制制度对个体思想的毒化,以及别里科夫这样的角色如何成为阻碍社会进步的代表"));
        books3.add(new Book(6L, "装在套子里的人", "短篇小说", 100, "深刻地揭示了专制制度对个体思想的毒化,以及别里科夫这样的角色如何成为阻碍社会进步的代表"));

        author1.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);

        List<Author> authorList = new ArrayList<>(Arrays.asList(author1, author2, author3, author4));
        return authorList;
    }

4. 中间操作

4.1 筛选与切片

方法描述
filter(Predicate predicate)可以对流中的元素进行条件过滤,符合过滤条件的才能继续留在流中(Predicate为True)
distinct()可以去除流中的重复元素(通过元素的 hashCode 和 equals 去重)
limit(long maxSize)截断流,使流中元素不超过给定数量maxSize(超出的部分将被抛弃),若数量不足则全保留
skip(long n)跳过流中的前n个元素,返回剩余元素的流;若流中元素不足n个,则返回空流

(1)filter

打印所有姓名长度大于2的作家的姓名。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        //匿名内部类
        authors.stream()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getName().length() > 2;
                    }
                })
                .forEach(author -> System.out.println(author.getName()));
        //Lambda
        authors.stream()
                .filter(author -> author.getName().length() > 2)
                .forEach(author -> System.out.println(author.getName()));

    }

(2)distinct

distinct方法通过元素的 hashCode 和 equals 方法判断去重,只有两元素的equals返回True且同时hashCode也相等才认为是相同元素。注意使用时要重写 hashCode 和 equals,逻辑上来说:

  • equals 方法判断两个对象是相等的,那这两个对象的 hashCode 值也一定要相等;
  • 两个对象有相同的 hashCode 值,他们也不一定是相等的(哈希碰撞);

打印所有作家的姓名,并且要求其中不能有重复元素。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        authors.stream()
                .distinct()
                .forEach(author -> System.out.println(author.getName()));

    }

(3)limit

打印流中前两个作家的姓名。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        authors.stream()
                .limit(2)
                .forEach(author -> System.out.println(author.getName()));

    }

(4)skip

打印流中除第一个作家外的剩余作家的姓名。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        authors.stream()
                .skip(1)
                .forEach(author -> System.out.println(author.getName()));

    }

4.2 映射

方法描述
map(Function mapper)接收一个计算函数,该函数会被应用到每个元素上,并将其映射/转换成一个新的元素(一对一,多流独立)
flatMap(Function mapper)接收一个计算函数,将流中的每个元素都转换成另一个流,并把所有流连接成一个流(一对多,多流混合)

(1)map

  • 提取作家的姓名长度并筛选长度大于2的值。
public static void main(String[] args) {
        List<Author> authors = getAuthors();
    	//匿名内部类
        authors.stream()
                .map(new Function<Author, Integer>() {
                    @Override
                    public Integer apply(Author author) {
                        return author.getName().length();
                    }
                })
                .filter(new Predicate<Integer>() {
                    @Override
                    public boolean test(Integer len) {
                        return len > 2;
                    }
                })
                .forEach(val -> System.out.println(val));
    	//Lambda
        authors.stream()
                .map(author -> author.getName().length())
                .filter(len -> len > 2)
                .forEach(val -> System.out.println(val));
    }
  • 提取作家的年龄并转换为10年后的值。
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 匿名内部类
        authors.stream()
                .map(new Function<Author, Integer>() {
                    @Override
                    public Integer apply(Author author) {
                        return author.getAge();
                    }
                })
                .map(new Function<Integer, Integer>() {
                    @Override
                    public Integer apply(Integer age) {
                        return age + 10;
                    }
                })
                .forEach(age-> System.out.println(age));
        //Lambda
        authors.stream()
                .map(author -> author.getAge())
                .map(age -> age + 10)
                .forEach(age-> System.out.println(age));
    }

(2)flatMap

  • 打印所有书籍的名字,要求对重复的元素进行去重。
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 匿名内部类
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() { // Author -> Stream<Book>
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .distinct()
                .forEach(book -> System.out.println(book.getName()));
        //Lambda
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(book -> System.out.println(book.getName()));
    }
  • 打印现有书籍的所有独立分类,并对分类进行去重(分类中不能出现 ‘,’ )
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 匿名内部类
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() { // Author -> Stream<Book>
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .distinct()
                .flatMap(new Function<Book, Stream<String>>() { // Book -> Stream<String>
                    @Override
                    public Stream<String> apply(Book book) {
                        return Arrays.stream(book.getCategory().split(","));
                    }
                })
                .distinct()
                .forEach(category-> System.out.println(category));
        //Lambda
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(category-> System.out.println(category));
    }

4.3 排序

方法描述
sorted()对流中元素按默认规则排序(类内必须实现Comparable接口)
sorted(Comparator com)对流中元素按比较器规则排序

对流中的元素按照年龄进行降序排序,并且要求不能有重复的元素。

(1)实现Comparable接口

//实现Comparable接口
public class Author implements Comparable<Author> {
    //id
    private Long id;
    //姓名
    private String name;
    //年龄
    private Integer age;
    //国家
    private String country;
    //作品
    private List<Book> books;

    //负整数、零或正整数,因为此对象小于、等于或大于指定对象。
    @Override
    public int compareTo(Author another) {
        return another.getAge() - this.getAge();
    }
}

//降序排序
public static void main(String[] args) {
	List<Author> authors = getAuthors();
	authors.stream()
			.distinct()
			.sorted()
			.forEach(author -> System.out.println(author.getAge()));
}

(2)自定义比较器Comparator

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 匿名内部类
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o2.getAge() - o1.getAge();
                    }
                })
                .forEach(author -> System.out.println(author.getAge()));
        //Lambda
        authors.stream()
                .distinct()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .forEach(author -> System.out.println(author.getAge()));
    }

5. 终止操作

5.1 遍历与统计

方法描述
void forEach(Consumer action)迭代,遍历流中的每个元素并执行 action
long count()返回当前流中的元素总数
Optional max(Comparator c)返回当前流中的最大值Optional
Optional min(Comparator c)返回当前流中的最小值Optional

(1)count

打印作家所出的所有书籍的数目,注意删除重复元素。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        long count = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .count();
        System.out.println(count);
    }

(2)max&min

分别获取这些作家的所出书籍的最高分和最低分并打印。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        Optional<Integer> max = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getScore())
                .max((score1, score2) -> score1 - score2);
        Optional<Integer> min = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getScore())
                .min((score1, score2) -> score1 - score2);
        System.out.println(max.get());
        System.out.println(min.get());
    }

5.2 匹配与查找

方法描述
boolean anyMatch(Predicate p)检查流中是否至少包含一个满足匹配条件的元素
boolean allMatch(Predicate p)检查流中所有元素是否都满足匹配条件
boolean noneMatch(Predicate p)检查流中所有元素是否都不满足匹配条件
Optional findFirst()返回当前流中的第一个元素(可能为空)
Optional findAny()返回当前流中的任意一个元素,无法保证获取的是流中首位元素(可能为空)

(1)anyMatch

判断是否有年龄在29以上的作家。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 匿名内部类
        boolean flag = authors.stream()
                .anyMatch(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() > 80;
                    }
                });
        System.out.println(flag);
        //Lambda
        boolean flag_lambda = authors.stream()
                .anyMatch(author -> author.getAge() > 80);
        System.out.println(flag_lambda);
    }

(2)allMatch

判断是否所有的作家都是成年人。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        boolean flag = authors.stream()
                .allMatch(author -> author.getAge() >= 18);
        System.out.println(flag);
    }

(3)noneMatch

判断作家的书籍列表是否都没有超过3本(包含重复)。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        boolean flag = authors.stream()
                .noneMatch(author -> author.getBooks().size() > 3);
        System.out.println(flag);
    }

(4)findFirst

获取一个年龄最大的作家,并输出他的姓名。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        Optional<Author> first = authors.stream()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .findFirst();
        first.ifPresent(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName());
            }
        });
    }

(5)findAny

获取任意一个年龄大于18的作家,如果存在就输出他的名字。

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        Optional<Author> optionalAuthor = authors.stream()
                .filter(author -> author.getAge()>18)
                .findAny();
        optionalAuthor.ifPresent(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName());
            }
        });
    }

5.3 收集

方法描述
collect(Collector c)将流中的元素转换为另一种数据存储形式。Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map);除此之外,Collectors 实用类提供了很多现成的静态方法来创建常见收集器实例。

参考文章: Java Stream collect

本节不考虑自定义Collector接口实现类的方式,只介绍结合Collectors工具类的主要使用方法,主要包括集合转换、分组分区两大功能模块,其中collect、Collector、Collectors的区别和关联如下。

在这里插入图片描述

5.3.1 集合转换
方法描述
Collectors.toList将流中的元素收集到一个List中
Collectors.toSet将流中的元素收集到一个Set中(自动去重)
Collectors.toCollection将流中的元素收集到一个Collection中
Collectors.toMap将流中的元素映射收集到一个Map中(若包含相同key,则需提供第三参数)
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 1.获取一个存放所有作者名字的List集合
        List<String> nameList = authors.stream()
                .map(author -> author.getName())
                .collect(Collectors.toList());
        System.out.println(nameList);
        // 2.获取一个所有书名的Set集合(自动去重)
        Set<Book> books = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .collect(Collectors.toSet());
        System.out.println(books);
        // 3.获取一个Map集合,map的key为作者名,value为List<Book>(若包含相同key,则需提供第三参数否则报错)
        Map<String, List<Book>> map = authors.stream()
                .distinct()
                .collect(Collectors.toMap(author -> author.getName(), author ->
                        author.getBooks()));
        System.out.println(map);
        // 4.获取一个存放所有作者名字的ArrayList集合(指定具体容器类型)
        ArrayList<String> nameArrList = authors.stream()
                .map(author -> author.getName())
                .collect(Collectors.toCollection(ArrayList::new));
        System.out.println(nameArrList);
    }
5.3.2 分组分区
方法含义说明
Collectors.groupingBy根据给定的分组函数的值进行分组,输出一个Map对象
Collectors.partitioningBy根据给定的分区函数的值进行分区,输出一个Map对象,且key始终为布尔值类型

Collectors.partitioningBy()分区收集器的使用方式与Collectors.groupingBy()分组收集器的使用方式基本相同,其区别在于划分维度,其中分组收集器可以有多个组的划分,而分区收集器只会有两个区( true 和 false) 的划分;单纯从使用维度来看,若分组收集器的分组函数返回值为布尔值,则其效果等同于一个分区收集器。因此本节只有着重介绍分组收集器groupingBy的使用。

(1)方法概述

groupingBy()操作需要指定三个关键输入,即分组函数分组容器值收集器

  • 分组函数:一个处理函数,用于基于指定的元素进行处理,返回一个用于分组的值(即分组结果Map的Key值),对于经过此函数处理后返回值相同的元素,将被分配到同一个组里;
  • 容器函数:一个生成容器的函数,当调用该函数时,生成所需类型的新空Map;
  • 值收集器:对于分组后的数据元素的进一步处理转换逻辑,即value的处理和存储逻辑,此处还是一个常规的Collector收集器,和collect()方法中传入的收集器完全等同,多用于实现分组数据统计、多级分组等操作;

为了方便使用,在Collectors工具类中提供了三个groupingBy重载实现,其源码如下:

//单参数:需要传入classifier分组函数,默认使用了HashMap作为容器函数,使用了toList()作为值收集器
public static <T, K> Collector<T, ?, Map<K, List<T>>>
groupingBy(Function<? super T, ? extends K> classifier) {
	return groupingBy(classifier, toList());
}
//双参数:需要传入classifier分组函数和downstream值收集器
public static <T, K, A, D>
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
                                      Collector<? super T, A, D> downstream) {
	return groupingBy(classifier, HashMap::new, downstream);
}
//三参数:可用于指定自定义Map容器,比如LinkedHashMap::new
public static <T, K, D, A, M extends Map<K, D>>
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
                              Supplier<M> mapFactory,
                              Collector<? super T, A, D> downstream) {
	//...
}

(2)实例分析

public class Test {
    public static void main(String[] args) {
        List<Student> students = getStudents();
        // 1. 按照单维度分组:按照是否成年分组(匿名函数)
        Map<String, List<Student>> collect1 = students.stream()
                .collect(Collectors.groupingBy(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) {
                        if (student.getAge() < 18) {
                            return "未成年";
                        } else {
                            return "已成年";
                        }
                    }
                }));
        System.out.println(collect1);

        // 2. 分组与组内数据的处理:统计男女性别各有多少人(lambda)
        Map<String, Long> collect2 = students.stream()
                .collect(Collectors.groupingBy(student -> {
                    if (student.getGender() == 1) {
                        return "男";
                    } else {
                        return "女";
                    }
                }, Collectors.counting()));
        System.out.println(collect2);

        // 3. 多级分组:先按国家分组,然后按照成绩是否及格分组
        Map<String, Map<String, List<Student>>> collect3 = students.stream()
                .collect(Collectors.groupingBy(student -> student.getCountry(),
                        Collectors.groupingBy(student -> {
                            if (student.getScore() >= 60) {
                                return "及格";
                            } else {
                                return "不及格";
                            }
                        }))
                );
        System.out.println(collect3);

    }

    private static List<Student> getStudents() {
        //数据初始化
        Student student1 = new Student(1,"张三",1,"祖安",17,88);
        Student student2 = new Student(2,"王雪",0,"祖安",23,92);
        Student student3 = new Student(3,"李四",1,"德玛西亚",31,74);
        Student student4 = new Student(4,"赵一",1,"祖安",25,58);
        Student student5 = new Student(5,"安琪拉",0,"德玛西亚",19,66);
        Student student6 = new Student(6,"大桥",0,"德玛西亚",13,86);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
        students.add(student5);
        students.add(student6);

        return students;
    }
}

5.4 归并

方法描述
reduce(T identity, BinaryOperator accumulator)可以将流中元素反复结合起来,得到一个值,返回T。其中,identity是初始值,accumulator是归并计算方法。
reduce(BinaryOperator accumulator)可以将流中元素反复结合起来,得到一个值,返回 Optional<T>。其中,accumulator是归并计算方法,初始值默认为流中第一个元素

(1)双参方法源码

T result = identity;
for (T element : this stream)
	result = accumulator.apply(result, element)
return result;

(2)单参方法源码

boolean foundAny = false;
T result = null;
for (T element : this stream) {
	if (!foundAny) {
		foundAny = true;
		result = element;
	}
	else
		result = accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty();

(3)实例分析

    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 使用reduce求所有作者年龄的和(匿名函数)
        Integer sum = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(0, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result + element;
                    }
                });
        System.out.println(sum);
        // 使用reduce求所有作者中年龄的最大值
        Integer max = authors.stream()
                .map(author -> author.getAge())
                .reduce(Integer.MIN_VALUE, (result, element) -> result < element ? element : result);
        System.out.println(max);
        // 使用reduce求所有作者中年龄的最小值(单参方法)
        Optional<Integer> minOptional = authors.stream()
                .map(author -> author.getAge())
                .reduce((result, element) -> result > element ? element : result);
        minOptional.ifPresent(age-> System.out.println(age));
    }

6. 并行流

当流中有大量元素时,我们可以使用并行流去提高操作的效率,其本质就是把任务拆分并分配给多个线程去完成,核心是通过Fork/Join框架实现。获取并行流的方式有两种:

  • parallel(): 把串行流转换成并行流;
  • parallelStream(): 从数据源中直接获取并行流对象;

在这里插入图片描述

    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        /**
         *  peek(Consumer c)
         *      - 中间操作: 对流中的每个元素执行操作,而不影响流的整体后续处理流程
         *      - 特点: 不改变流的原始内容,也不决定流的最终输出结果
         *      - 用途: action动作会在流的每个元素上执行一次,但具体执行多少次取决于下游的终端操作
         *      - 注意:
         *          - 对于并行流,peek()操作的执行顺序没有保证,而且可能会多次执行
         *          - peek()通常用于读取或打印流元素,而不是修改它们。虽然理论上可以尝试在peek()中修改元素,但由于流的惰性求值和可能的不可变性,这样的修改可能不会反映到源集合或后续流操作中。
         */
        //1. parallel转化
        Integer sum = stream.parallel()
                .peek(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer num) {
                        System.out.println(num+Thread.currentThread().getName());
                    }
                })
                .filter(num -> num > 5)
                .reduce((result, ele) -> result + ele)
                .get();
        System.out.println(sum);
        //2. parallelStream直接获取
        List<Author> authors = getAuthors();
        authors.parallelStream()
                .map(author -> author.getAge())
                .map(age -> age + 10)
                .filter(age->age>18)
                .map(age->age+2)
                .forEach(System.out::println);
    }

二. Optional

1.Optional概述

我们在编写代码的时候出现最多的就是空指针异常(NPE),所以在很多情况下需要人为做各种非空的判断,但这种方式会让代码变得臃肿不堪、难以理解;为了解决这个问题,JDK8中引入了Optional容器类,用于封装结果或数据,并提供一系列空值检测和处理方法,这使得我们可以通过更优雅的方式来避免空指针异常情况。

注意: Mybatis 3.5版本已支持Optional,当dao方法的返回值类型定义成Optional类型时,MyBastis会自动将数据封装成Optional对象返回。

2.Optional使用

2.1 创建对象

方法描述
Optional.empty返回一个空的 Optional 实例,其封装数据为null
Optional.of将指定值用 Optional 封装之后返回,如果该值为 null,则会抛出一个 NullPointerException 异常
Optional.ofNullable将指定值用 Optional 封装之后返回,如果该值为 null,则返回一个空的 Optional 对象(无异常)
Author author = getAuthor();
// 1.一般使用Optional的静态方法ofNullable来把数据封装成一个Optional对象
Optional<Author> authorOptional1 = Optional.ofNullable(author);
// 2.当确定一个对象不是空的则可以使用Optional的静态方法of来把数据封装成Optional对象
Optional<Author> authorOptional2 = Optional.of(author);
// 3.通过静态工厂方法 Optional.empty(),创建一个空的 Optional 对象
Optional<Author> authorOptional3 = Optional.empty();

2.2 常用方法

方法描述
get如果该值存在,将该值返回,否则抛出一个 NoSuchElementException 异常(不安全)
ifPresent(Comsumer c)如果值存在,就执行使用该值的消费方法Comsumer,否则什么也不做
isPresent()如果值存在就返回 true,否则返回 false
orElse(T other)如果有值则将其返回,否则返回一个默认值other
orElseGet(Supplier other)如果有值则将其返回,否则返回一个由指定的 Supplier 接口生成的值
orElseThrow(Supplier exceptionSupplier)如果有值则将其返回,否则抛出一个由指定的 Supplier 接口生成的异常
filter如果值存在并且满足提供的谓词,就返回包含该值的 Optional 对象;否则返回一个空的 Optional 对象
map如果值存在,就对该值执行提供的mapping 函数调用
Optional<Author> authorOptional = Optional.ofNullable(getAuthor());
// 安全消费值
authorOptional.ifPresent(author ->System.out.println(author.getName()));
// 安全获取值
Author author1 = authorOptional.orElseGet(() -> new Author());

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1972183.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【前端】中后台框架 添加其他布局的探索

文章目录 前言需求整理第一步&#xff1a;实现可切换布局第二步&#xff1a;配置页面顶部的路由&#xff08;一级路由&#xff09;第三部&#xff1a;配置左侧二级和二级以上的路由第四部&#xff1a;给侧边栏加一个动画第五部&#xff1a;刷新页面之后顶部路由、左侧路由的回显…

5款免费写作生成软件,自动生成原创文章很简单

在人工智能时代的今天&#xff0c;创作者面对写作不再是一件令人望而生畏的事情。随着AI技术的不断发展&#xff0c;涌现出了许多优秀的免费写作生成软件&#xff0c;让自动生成原创文章变得轻松简单。以下为大家详细介绍5款备受赞誉的免费写作生成软件&#xff0c;下面跟随小编…

硬盘数据丢失不再怕,四大恢复工具帮你轻松逆转局面!

硬盘故障、误删文件、病毒攻击等原因导致数据丢失的情况时有发生。面对这种情况&#xff0c;如何高效、快速地进行硬盘数据恢复呢&#xff1f;接下来几款好用的数据恢复软件推荐给大家。 一、福昕数据恢复&#xff1a;全方位恢复&#xff0c;让数据无遗漏 链接&#xff1a;ww…

手把手教你OpenCV实现实时人脸检测(C++)

目录 1&#xff0c;原理介绍 2&#xff0c;代码讲解 3&#xff0c;全部代码 4&#xff0c;结果展示 1&#xff0c;原理介绍 haarcascade_frontalface_default.xml 是一个 XML 文件&#xff0c;它包含了使用 Haar 特征分类器训练得到的人脸检测模型。这个模型是 OpenCV 库自…

【小知识】站在前人的肩膀上写程序——STL库初阶算法函数的使用

【小知识】站在前人的肩膀上写程序——STL库初阶算法函数的使用 1.墨水瓶算法和swap函数2.打擂台算法和max&#xff0c;min函数3.排序——sort函数 1.墨水瓶算法和swap函数 如果想交换两个墨水瓶的墨水该怎么办呢&#xff1f;我们可以准备第三个墨水瓶。将第一个墨水瓶的墨水倒…

linux安装配置jdk

①下载jdk安装包&#xff0c;放在/opt/app/software/java下 cd /opt/app/software/java②进行解压操作 tar -zxvf jdk-8u251-linux-x64.tar.gz③解压完成之后&#xff0c;进行环境变量的配置&#xff0c;shell下执行 vi ~/.bash_profile根据jdk的安装目录&#xff0c;加入 …

LeeCode Practice Journal | Day31_GA05

56. 合并区间 题目&#xff1a;56. 合并区间 - 力扣&#xff08;LeetCode&#xff09; 题解&#xff1a;代码随想录 (programmercarl.com) 思路很清晰&#xff0c;对数组的操作稀烂&#xff0c;细节上也出现很多问题 solution public class Solution {public int[][] Merge(in…

2024全新Thinkphp聊天室H5实时聊天室群聊聊天室自动分配账户完群组/私聊/禁言等功能/全开源运营版本

全开源运营版本聊天室H5实时聊天室群聊聊天室自动分配账户完群组/私聊/禁言等功能 运营版本的聊天室&#xff0c;可以添加好友&#xff0c;建立群组&#xff0c;私聊&#xff0c;禁言功能 H5TP5.0mysqlPHP 源码开源不加密

【python】Python二手房住房数据抓取可视化(源码+数据集+论文)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

python爬取豆瓣电影top250-python实战项目,手把手教学,附源码

目录 1、分析网页2、请求服务器 2.1导入包2.2设置浏览器代理2.3请求服务器格式2.4请求服务器代码汇总 3.xpath提取信息 3.1获取xpath节点的方法3.2xpath提取内容3.2.1提取文本3.2.2提取链接3.2.3提取标签元素 4.正则表达式 4.1提取固定位置的信息4.2匹配出数字 5、提取一页中的…

Java 并发编程:Java 线程池的介绍与使用

大家好&#xff0c;我是栗筝i&#xff0c;这篇文章是我的 “栗筝i 的 Java 技术栈” 专栏的第 024 篇文章&#xff0c;在 “栗筝i 的 Java 技术栈” 这个专栏中我会持续为大家更新 Java 技术相关全套技术栈内容。专栏的主要目标是已经有一定 Java 开发经验&#xff0c;并希望进…

细分 Insight 合作伙伴 2024 年企业技术状况报告

Insight Partners 的团队刚刚发布了 2024 年企业技术状况报告。在 60 幻灯片中有很多东西可以消耗&#xff0c;但我们挑选了应该让我们的观众感兴趣的东西 - 坦率地说&#xff0c;有很多有趣的东西。我将把调查方法的东西留给你使用&#xff0c;但足以说样本量很大&#xff0c;…

dami支付漏洞

使用burpsuite等抓包工具&#xff0c;抓取数据包后&#xff0c;修改数据包中的参数从而达到支付篡改的目的&#xff1b;篡 改的参数&#xff1a;商品ID&#xff0c;购买价格&#xff0c;购买数量&#xff0c;手机号码&#xff0c;订单D&#xff0c;支付状态 常见漏洞利用手段…

国家网络身份个人认证方法

申领网络身份认证后&#xff0c;用户会得到一张虚拟的“网络身份证”&#xff0c;它可以向需要实名认证的互联网平台进行认证&#xff0c;不再需要输入姓名和身份证号等信息。 申请方式&#xff1a;各手机应用平台搜索国家网络身份认证即可&#xff08;必须支持NFC才能申请&am…

AI产品经理必备:什么是LLM,有什么优劣势

LLM&#xff08;Large Language Model大型语言模型&#xff09;是一种人工智能技术&#xff0c;能够理解和生成自然语言文本。LLM可以应用于多种场景&#xff0c;包括自然语言理解、文本生成、机器翻译、对话系统、问答系统、文本摘要、情感分析等。可以帮助人们快速生成文章、…

c# 构造器的声明与调用

在C#中&#xff0c;构造器&#xff08;Constructor&#xff09;是一种特殊类型的函数&#xff0c;用于初始化类的新实例。构造器的名字必须与类名完全相同&#xff0c;并且没有返回类型&#xff0c;甚至连void也不行。 当创建类的一个新实例时&#xff0c;构造器会自动被调用。…

全球轻型电动轮椅市场规划预测:未来六年CAGR为7.3%

随着全球人口老龄化的加剧和消费者对便捷、高效出行工具的需求增加&#xff0c;轻型电动轮椅作为提升行动不便人士生活质量的重要工具&#xff0c;正逐渐受到市场的广泛关注。本文旨在通过深度分析轻型电动轮椅行业的各个维度&#xff0c;揭示行业发展趋势和潜在机会。 【市场…

StudyStudyStudy第十六天(2024.8.2)

1.代理模式 代理模式分为静态代理和动态代理 代理模式&#xff0c;就是在想要执行的代码之前或之后添加代码块&#xff0c;并不会破坏原有的代码结构。可以理解成加上了一个访问层 1.静态代理 创建一个接口Shopping public interface Shopping {void shopping(); }创建一个…

笔记:唐老师讲电赛之唐老师讲电子器件(1)电阻 参数与选型

电阻 a . 精度 电阻----运放中的电阻要选精度高的&#xff0c;一般0.1% 若在设计电路中电路参数由某个电阻决定&#xff0c;则需要选取高精度电阻。例如&#xff0c;反向放大器等对于反馈系数、增益等参数完全由电阻决定的&#xff0c;则需要选取精度较高的电阻&#xff0c;…

PDF文件点击打印无反应?是何原因造成能解决吗?

PDF无法打印怎么处理&#xff1f;在我们工作中&#xff0c;经常会遇见各种各样的文件问题&#xff0c;当我们想要将PDF文件打印出来纸质版使用&#xff0c;却不知什么原因&#xff0c;显示PDF无法打印&#xff0c;这时应该怎么处理呢&#xff1f; 一般情况下&#xff0c;PDF文件…