企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 3.3 流API 笔者在这里对常见的流API做了一个简单的归纳,但是本节并不打算全部讲解——那样只会让我们文章的内容变得繁琐。 笔者会在本节对几个较难理解的点进行介绍。以下是其常见的Stream API: - 筛选/过滤 - filter - distinct - limit - skip - 映射 - map - flatMap - 查找/匹配 - allMatch - anyMatch - noneMatch - findFirst - findAny - `Optional<T>` - `isPresent()`:返回是否包含值 - `ifPresent(Consumer block)`:在值存在时执行block的代码块 - `T get()`:值存在时返回值,否则抛出`NoSuchElementException` - `T orElse(T other)`:值不存在时返回other - 归纳 - reduce ---- | 操作 | 操作类型 | 返回类型 | 入参类型 | 函数描述符 | | :----: | :----: | :----: | :----: | :----: | | **filter** | 中间 | `Stream<T>` | `Predicate<T>` | `T->boolean` | | **distinct** | 中间(有状态,无界) | `Stream<T>` | | | | **skip** | 中间(有状态,有界) | `Stream<T>` | long | | | **limit** | 中间(有状态,有界) | `Stream<T>` | long` | | | **map** | 中间 | `Stream<R>` | `Function` `<T, R>` | `T->R` | | **flatMap** | 中间 | `Stream<R>` | `Function` `<T, Stream<R>>` | `T->Stream<R>` | | **sorted** | 中间(有状态,无界) | `Stream<T>` | `Comparator<T>` | `(T, T)->int` | | **anyMatch** | 终端 | `boolean` | `Predicate<T>` | `T->boolean` | | **noneMatch** | 终端 | `boolean` | `Predicate<T>` | `T->boolean` | | **anyMatch** | 终端 | `boolean` | `Predicate<T>` | `T->boolean` | | **allMatch** | 终端 | `boolean` | `Predicate<T>` | `T->boolean` | | **findAny** | 终端 | `Optional<T>` | | | | **findFirst** | 终端 | `Optional<T>` | | | | **forEach** | 终端 | `void` | `Consumer<T>` | `T->void` | | **collect** | 终端 | `R` | `Collector` `<T, A, R>` | | | **reduce** | 终端(有状态,有界) | `Optional<T>` | `BinaryOPerator` `<T>` | `(T, T)->T` | | **count** | 终端 | `long` | | |