Class StreamUtil
java.lang.Object
com.sure.tool.util.StreamUtil
流工具类,参考 Hutool 的
StreamUtil 设计。
提供数组 / 集合 / 迭代器 / Optional 转流、多流合并、空安全等常用静态方法,
全部方法对 null 输入安全(不会抛 NPE)。
- Since:
- 1.19.0
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Stream<T> 合并多个流(null流自动跳过)。static <T> Stream<T> empty()返回空流。static <T> Stream<T> nullToEmpty(Stream<T> stream) 空安全:null流转换为空流。static <T> Stream<T> 迭代器转流(null安全)。static <T> Stream<T> of(Collection<T> collection) 集合转流(null安全)。static <T> Stream<T> Optional 转流:有值则单元素流,否则空流(Java 9Stream.ofNullable语义)。static <T> Stream<T> of(T... values) 变参或数组转流(null安全)。
-
Method Details
-
of
变参或数组转流(null安全)。示例:
StreamUtil.of("a", "b", "c");StreamUtil.of(null)返回空流。- Parameters:
values- 元素数组,可为null- Returns:
- 元素流;
values为null返回空流
-
of
集合转流(null安全)。示例:
StreamUtil.of(list);StreamUtil.of(null)返回空流。- Parameters:
collection- 集合,可为null- Returns:
- 集合元素流;
null返回空流
-
of
-
of
-
concat
合并多个流(null流自动跳过)。示例:
StreamUtil.concat(Stream.of(1), null, Stream.of(2))产生 1、2。- Parameters:
streams- 流数组,可为null,元素可为null- Returns:
- 合并后的流;全部为空返回空流
-
nullToEmpty
-
empty
-