JAVA8新特性——Stream

Stream流的出现极大的方便了我们对数据的处理,作为处理数据的一种通用方式。它提供了一种高效且灵活的方法来执行诸如过滤、映射、汇总等操作。Stream API 可以用于任何实现了 Iterable 接口的数据结构,或者能够转换为支持流式处理的对象。

以下是一些常见的可以与 Stream API 结合使用的数据结构或容器:

集合框架:包括但不限于 List, Set, Map 等。
List: 比如 ArrayList, LinkedList
Set: 比如 HashSet, TreeSet
Map: 比如HashMap, TreeMap,通常会用 map.values()map.entrySet() 转换为流。
数组:数组可以通过 Arrays.stream(T[])或者Stream.of(T[]) 转换为流。
IO 操作:例如 Files.lines(Path) 可以从文件中读取每一行作为流中的元素。
生成器:通过 Stream.generate(Supplier<T>) 可以创建无限流或有限流。
并行数据源:如 IntStream.range(int startInclusive, int endExclusive) 可用于生成整数范围内的值流。
自定义数据源:任何实现了Iterable 接口的类都可以产生流。

Stream API的分类

我们一般把stream的操作分为中间操作和终结操作

中间操作

filtermapdistinctsortedlimitskipflatMap

终结操作

forEachcountminmaxcollectanyMatchallMatchnoneMatchfindAnyfindFirstreduce

根据对数据操作形式的不同我们也可以对操作进行分类
过滤操作(Filtering)
filter(Predicate predicate):选择符合给定谓词的元素。
映射操作(Mapping)
map(Function<T, R> mapper):将流中的每个元素映射为另一个类型。
flatMap(Function<T, Stream> mapper):将流中的每个值都转换为一个流,然后将所有的流连接成一个流。
去重操作(Deduplication)
distinct():删除重复的元素(基于 equals() 方法)。
排序操作(Sorting)
sorted():默认使用自然排序(如果实现了 Comparable)。
sorted(Comparator comparator):根据提供的比较器进行排序。
限制操作(Limiting)
limit(long maxSize):截取流中的前 n 个元素。
跳过操作(Skipping)
skip(long n):跳过流中的前 n 个元素。
展平操作(Flattening)
flatMap(Function<T, Stream> mapper):用于将嵌套的流展平成一个单一的流。
聚合操作(Aggregation)

  • forEach(Consumer action):对流中的每个元素执行一个动作。
  • count():返回流中的元素数量。
  • min(Comparator comparator):找到流中的最小元素。
  • max(Comparator comparator):找到流中的最大元素。
  • anyMatch(Predicate predicate):检查是否有任何元素符合给定的谓词。
  • allMatch(Predicate predicate):检查是否所有的元素都符合给定的谓词。
  • noneMatch(Predicate predicate):检查是否没有任何元素符合给定的谓词。
  • findAny():找到流中的任意元素(如果有多个并行流,则是任意一个)。
  • findFirst():找到流中的第一个元素(如果存在的话)。
  • reduce(BinaryOperator) 或 reduce(T identity, BinaryOperator):将流中的元素归约为一个单一的结果。
    收集操作(Collecting)
    collect(Collector<T,A,R> collector):将流转换为其他形式的数据结构,如列表、集合、字符串等。

注意事项

  • 惰性求值(如果没有终结操作,中间操作是不会得到执行的)
  • 流是一次性的(一旦一个流对象经过终结操作后,这个流就不能被使用)
  • 不会影响原数据(在流中我们可以对数据进行很多处理,但一般不会影响原来集合的元素,除了对原数据进行set方法)

demo


    private static void test19() {
        List<Author> authors = getAuthors();
        Optional<Integer> reduce = authors.stream()
                .map(author -> author.getAge())
                .reduce((integer,integer2) -> integer < integer2 ? integer : integer2);
        reduce.ifPresent(System.out::println);
    }

    private static void test18() {
        List<Author> authors = getAuthors();
        Integer reduce = authors.stream()
                .map(author -> author.getAge())
                .reduce(Integer.MIN_VALUE, (result, element) -> result > element ? result : element);
        System.out.println(reduce);
    }

    private static void test17() {
        List<Author> authors = getAuthors();
        Integer sum = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(0, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                });
        System.out.println(sum);
    }


    private static void test16() {
        List<Author> authors = getAuthors();
        Optional<Author> first = authors.stream()
                .sorted((o1, o2) -> o1.getAge() - o2.getAge())
                .findFirst();
        System.out.println(first.get().getName());
    }
    private static void test15() {
        List<Author> authors = getAuthors();
        Optional<Author> optionalAuthor = authors.stream()
                .filter(author -> author.getAge() > 18)
                .findAny();
        System.out.println(optionalAuthor.get().getName());
        optionalAuthor.ifPresent(author -> System.out.println(author.getName()));
    }

    private static void test14() {
        List<Author> authors = getAuthors();
        boolean b = authors.stream()
                .anyMatch(author -> author.getAge() > 29);
        System.out.println(b);
    }

    private static void test13() {
        List<Author> authors = getAuthors();
        Map<String, List<Book>> collect = authors.stream()
                .distinct()
                .collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));
        System.out.println(collect);
    }

    private static void test12() {
        List<Author> authors = getAuthors();
        Set<String> set = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getName())
                .collect(Collectors.toSet());
        System.out.println(set);
    }

    private static void test11() {
        List<Author> authors = getAuthors();
        Set<String> collect = authors.stream()
                .map(author -> author.getName())
                .collect(Collectors.toSet());
        System.out.println(collect);
    }

    private static void test10() {
        List<Author> authors = getAuthors();
        Optional<Integer> min = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getScore())
                .min((o1, o2) -> o1 - o2);
        System.out.println(min.get());
    }

    private static void test09() {
        List<Author> authors = getAuthors();
        long count = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .count();
        System.out.println(count);

    }

    private static void test08() {
        List<Author> authors = getAuthors();
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(System.out::println);
    }

    public static void test02(){
        Integer[] arr = {1,2,3,4,5,6};
        Stream<Integer> stream = Arrays.stream(arr);
        Stream<Integer> arr1 = Stream.of(arr);
        stream.filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer < 4;
            }
        }).forEach(integer -> System.out.println(integer));
    }
    public static void test01(){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("蜡笔小新",18);
        map.put("小黑子",17);
        map.put("大黑子",16);

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Stream<Map.Entry<String, Integer>> stream = entries.stream();
        stream.filter(stringIntegerEntry -> stringIntegerEntry.getValue() > 17)
                .forEach(System.out::println);

        Stream<String> stream1 = map.keySet().stream();
        stream1.forEach(System.out::println);
    }

    /**
     * map()方法的使用
     */
    public static void test03() {
        List<Author> authors = getAuthors();
        /*authors.stream().filter(author -> author.getName().length() > 1)
                .forEach(author -> System.out.println(author.getName()));
*/
        authors.stream()
                .map(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }).forEach(System.out::println);
    }

    /**
     * sorted方法的使用
     */
    public static void test04(){
        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()));
    }

    /**
     * limit方法的使用
     */
    public static void test05(){
        List<Author> authors = getAuthors();
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o2.getAge() - o1.getAge();
                    }
                    //limit限制流里面元素的个数
                }).limit(2).forEach(author -> System.out.println(author.getName()));
    }

    public static void test06(){
        getAuthors().stream()
                .distinct()
                .sorted()
                .skip(1)  //跳过前1个元素
                .forEach(author -> System.out.println(author.getName()));
    }

    public static void test07(){
        List<Author> authors = getAuthors();
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .distinct()
                .forEach(new Consumer<Book>() {
                    @Override
                    public void accept(Book book) {
                        System.out.println(book.getName());
                    }
                });

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光阿盖尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值