设为首页 加入收藏

TOP

JAVA函数式编程(五)
2023-08-06 07:49:34 】 浏览:91
Tags:JAVA
定时任务执行"); } }; Thread thread = new Thread(runnable); // invokeDynamic指令实现的(反汇编) // MethodHandle Runnable r = () -> System.out.println("定时任务执行"); Thread t = new Thread(r); } }

理解Stream

Stream是用于对集合(Collection)进行处理和操作的工具,让我们能够以声明式的方式进行数据的转换、过滤、映射等操作。Stream 可以看作是对集合进行高级迭代和函数式处理的工具,它提供了一种更现代、更简洁的方式来操作数据,使得代码更加清晰、易读、易维护。

Stream API 提供了一系列操作,这些操作分为两类:

  1. Intermediate(中间操作):这些操作返回一个新的 Stream,用于定义一系列的数据转换和处理操作,但并不立即执行。中间操作包括 filtermapdistinctsortedlimitskip 等。
  2. Terminal(终端操作):这些操作触发 Stream 的处理,执行中间操作定义的一系列数据转换和处理操作,生成最终的结果。终端操作包括 forEachcollectcountreduceminmaxanyMatchallMatchnoneMatch 等。

!!一般不会将Stream作为参数进行传递,但是如果有Stream作为参数进行传递的话,一定要进行串行与并行的判断,并且根据实际情况进行切换

    /**
     * Returns an equivalent stream that is sequential.  May return
     * itself, either because the stream was already sequential, or because
     * the underlying stream state was modified to be sequential.
     *
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
     * operation</a>.
     *
     * @return a sequential stream
     */
    S sequential();

    /**
     * Returns an equivalent stream that is parallel.  May return
     * itself, either because the stream was already parallel, or because
     * the underlying stream state was modified to be parallel.
     *
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
     * operation</a>.
     *
     * @return a parallel stream
     */
    S parallel();

示例使用 Stream API:

import java.util.Arrays;
import java.util.List;

public class StreamExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva");
        
        // 使用 Stream API 对集合进行操作
        long count = names.stream()
                         .filter(name -> name.length() <= 4)
                         .map(String::toUpperCase)
                         .sorted()
                         .count();
        System.out.println(count); // 输出:4
    }
}

在上面的示例中,我们使用 Stream API 对集合 names 进行一系列的操作:首先用 filter 过滤掉长度大于 4 的字符串,然后用 map 将字符串转换为大写,接着用 sorted 进行排序,最后用 count 终端操作统计符合条件的元素个数。

Stream API 的优势在于它具有懒加载的特性,即只有当终端操作触发时,中间操作才会执行。这使得 Stream 在处理大量数据时,能够有效地提高性能和资源利用率。

sorted的应用

public class C03_08_Stream {
    public static void main(String[] args) {
        sorted(Integer::compareTo, Arrays.asList(5, 1, 5, 4, 8, 9, 6, 5, 4, 8, 5, 4, 2, 3, 4)).stream().distinct().forEach(System.out::println);
    }
    /**
     * sorted<Comparator<? super T> comparator>的使用
     * @param comparator 比较器
     * @param collection 待排序的集合
     * @param <T>
     * @return 排序后的集合
     */
    private static <T> Collection<T> sorted(Comparator<T> comparator, Collection<T> collection) {
        return collection.stream().sorted(comparator).collect(Collectors.toList());
    }
}

map的应用

map主要是做一个映射,例如这就是将Integer类型转换成Long类型

reduce主要是合并操作,例如将这将多个元素求和

/**
     * Stream 的 map<Function>操作
     *
     * @param integers
     */
    private static void count(Integer... integers) {
        Stream.of(integers)
                .map(Long::valueOf)
                .reduce(Long::sum)
                .ifPresent(System.out::println);
    }

并行parallel

/**
     * 并行parallel案例
     * @param integers
     */
private static void parallelSort(Integer... integers){
    Stream.of(integers).sorted(Integer::compareTo).parallel().forEach(C03_08_Stream::println);
}

public static void println(Object obj){
    System.out.printf("[%s]:%s \n",Thread.currentTh
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇@ControllerAdvice 注解使用及原.. 下一篇《深入理解Java虚拟机》读书笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目