T
- 对象类型public class EasyStream<T> extends AbstractEnhancedWrappedStream<T,EasyStream<T>>
单元素的扩展流实现。基于原生Stream进行了封装和增强。
作者经对比了vavr、eclipse-collection、stream-ex以及其他语言的api,结合日常使用习惯,进行封装和拓展
Stream为集合提供了一些易用api,它让开发人员能使用声明式编程的方式去编写代码。
中间操作和结束操作
针对流的操作分为分为中间操作和结束操作,
流只有在结束操作时才会真正触发执行以往的中间操作。
中间操作:
串行流与并行流
流分为串行流和并行流两类:
Spliterator
拆分为多个异步任务ForkJoinTask
执行,
这些异步任务默认使用ForkJoinPool
线程池进行管理;
WrappedStream.sequential()
或WrappedStream.parallel()
互相转换。Stream
Modifier and Type | Class and Description |
---|---|
static interface |
EasyStream.Builder<T>
建造者
|
stream
NOT_FOUND_ELEMENT_INDEX
Modifier and Type | Method and Description |
---|---|
Opt<BigDecimal> |
avg(Function<? super T,BigDecimal> mapper)
计算
BigDecimal 类型的平均值 并以四舍五入的方式保留2位精度 |
Opt<BigDecimal> |
avg(Function<? super T,BigDecimal> mapper,
int scale)
BigDecimal 类型的平均值 并以四舍五入的方式保留小数点后scale位 |
Opt<BigDecimal> |
avg(Function<? super T,BigDecimal> mapper,
int scale,
RoundingMode roundingMode)
计算
BigDecimal 类型的平均值 |
OptionalDouble |
avg(ToDoubleFunction<? super T> mapper)
计算double类型的平均值
|
OptionalDouble |
avg(ToIntFunction<? super T> mapper)
计算int类型的平均值
|
OptionalDouble |
avg(ToLongFunction<? super T> mapper)
计算double平均值
|
static <T> EasyStream.Builder<T> |
builder()
返回
FastStream 的建造器 |
static <T> EasyStream<T> |
concat(Stream<? extends T> a,
Stream<? extends T> b)
创建一个惰性拼接流,其元素是第一个流的所有元素,然后是第二个流的所有元素。
|
static <T> EasyStream<T> |
empty()
返回空的串行流
|
static <T> EasyStream<T> |
generate(Supplier<T> s)
返回无限串行无序流
其中每一个元素都由给定的
Supplier 生成
适用场景在一些生成常量流、随机元素等 |
static <T> EasyStream<T> |
iterate(T seed,
Predicate<? super T> hasNext,
UnaryOperator<T> next)
返回无限有序流
该流由 初始值 然后判断条件 以及执行 迭代函数 进行迭代获取到元素
例如
FastStream.iterate(0, i -> i < 3, i -> ++i)
就可以创建包含元素0,1,2的流,使用WrappedStream.limit(long) 可以限制元素个数
|
static <T> EasyStream<T> |
iterate(T seed,
UnaryOperator<T> f)
返回无限有序流
该流由 初始值 以及执行 迭代函数 进行迭代获取到元素
例如
FastStream.iterate(0, i -> i + 1)
就可以创建从0开始,每次+1的无限流,使用WrappedStream.limit(long) 可以限制元素个数
|
static <T> EasyStream<T> |
iterateHierarchies(T root,
Function<T,Collection<T>> discoverer)
指定一个层级结构的根节点(通常是树或图),
然后获取包含根节点在内,根节点所有层级结构中的节点组成的流。
|
static <T> EasyStream<T> |
iterateHierarchies(T root,
Function<T,Collection<T>> discoverer,
Predicate<T> filter)
指定一个层级结构的根节点(通常是树或图),
然后获取包含根节点在内,根节点所有层级结构中的节点组成的流。
|
<R> EasyStream<R> |
map(Function<? super T,? extends R> mapper)
返回与指定函数将元素作为参数执行的结果组成的流
这是一个无状态中间操作
|
static <T> EasyStream<T> |
of(Iterable<T> iterable)
通过实现了
Iterable 接口的对象创建串行流,若输入对象为null ,则返回一个空的串行流 |
static <T> EasyStream<T> |
of(Iterable<T> iterable,
boolean parallel)
通过传入的
Iterable 创建流,若输入对象为null ,则返回一个空的串行流 |
static <T> EasyStream<T> |
of(Stream<T> stream)
通过传入的
Stream 创建流,若输入对象为null ,则返回一个空的串行流 |
static <T> EasyStream<T> |
of(T... values)
返回包含指定元素的串行流,若输入数组为
null 或空,则返回一个空的串行流 |
static <T> EasyStream<T> |
of(T t)
返回包含单个元素的串行流
|
static EasyStream<String> |
split(CharSequence str,
String regex)
拆分字符串,转换为串行流
|
<R extends Number> |
sum(Function<? super T,R> mapper)
计算
Number 类型的总和 |
double |
sum(ToDoubleFunction<? super T> mapper)
计算double类型的总和
|
int |
sum(ToIntFunction<? super T> mapper)
计算int类型的总和
|
long |
sum(ToLongFunction<? super T> mapper)
计算long类型的总和
|
EasyStream<T> |
wrap(Stream<T> stream)
根据一个原始的流,返回一个新包装类实例
|
equals, exec, hashCode, toString, unwrap
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
at, findFirst, findFirstIdx, findLast, findLast, findLastIdx, forEachIdx, forEachOrderedIdx, group, group, group, isEmpty, isNotEmpty, join, join, join, partition, partition, partition, toColl, toIdxMap, toIdxMap, toList, toMap, toMap, toMap, toMap, toSet, toUnmodifiableList, toUnmodifiableMap, toUnmodifiableMap, toUnmodifiableSet, toZip
append, distinct, dropWhile, filter, filterIdx, flat, flat, flatMap, flatMapIdx, flatNonNull, flatTree, log, mapIdx, mapMulti, mapNonNull, nonNull, parallel, peekIdx, prepend, push, reverse, splice, split, splitList, takeWhile, toEntries, toEntries, unshift, zip
allMatch, anyMatch, close, collect, collect, count, distinct, easyStream, filter, findAny, findFirst, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEachOrdered, isParallel, iterator, limit, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, onClose, parallel, peek, reduce, reduce, reduce, sequential, skip, sorted, sorted, spliterator, toArray, toArray, unordered
public static <T> EasyStream.Builder<T> builder()
FastStream
的建造器T
- 元素的类型public static <T> EasyStream<T> empty()
T
- 元素类型public static <T> EasyStream<T> of(T t)
T
- 元素类型t
- 单个元素@SafeVarargs public static <T> EasyStream<T> of(T... values)
null
或空,则返回一个空的串行流T
- 元素类型values
- 指定元素public static <T> EasyStream<T> of(Iterable<T> iterable)
Iterable
接口的对象创建串行流,若输入对象为null
,则返回一个空的串行流T
- 元素类型iterable
- 实现了Iterable
接口的对象public static <T> EasyStream<T> of(Iterable<T> iterable, boolean parallel)
Iterable
创建流,若输入对象为null
,则返回一个空的串行流T
- 元素类型iterable
- Iterable
parallel
- 是否并行public static <T> EasyStream<T> of(Stream<T> stream)
Stream
创建流,若输入对象为null
,则返回一个空的串行流T
- 元素类型stream
- Stream
public static <T> EasyStream<T> iterate(T seed, UnaryOperator<T> f)
例如
FastStream.iterate(0, i -> i + 1)
就可以创建从0开始,每次+1的无限流,使用WrappedStream.limit(long)
可以限制元素个数
T
- 元素类型seed
- 初始值f
- 用上一个元素作为参数执行并返回一个新的元素public static <T> EasyStream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
例如
FastStream.iterate(0, i -> i < 3, i -> ++i)
就可以创建包含元素0,1,2的流,使用WrappedStream.limit(long)
可以限制元素个数
T
- 元素类型seed
- 初始值hasNext
- 条件值next
- 用上一个元素作为参数执行并返回一个新的元素public static <T> EasyStream<T> iterateHierarchies(T root, Function<T,Collection<T>> discoverer, Predicate<T> filter)
指定一个层级结构的根节点(通常是树或图),
然后获取包含根节点在内,根节点所有层级结构中的节点组成的流。
该方法用于以平铺的方式对图或树节点进行访问,可以使用并行流提高效率。
eg:
Tree root = // 构建树结构
// 搜索树结构中所有级别为3的节点,并按权重排序
List<Tree> thirdLevelNodes = StreamUtil.iterateHierarchies(root, Tree:getChildren)
.filter(node -> node.getLevel() == 3)
.sorted(Comparator.comparing(Tree::getWeight))
.toList();
T
- 对象类型root
- 根节点discoverer
- 下一层级节点的获取方法filter
- 节点过滤器,不匹配的节点与以其作为根节点的子树将将会被忽略public static <T> EasyStream<T> iterateHierarchies(T root, Function<T,Collection<T>> discoverer)
指定一个层级结构的根节点(通常是树或图),
然后获取包含根节点在内,根节点所有层级结构中的节点组成的流。
该方法用于以平铺的方式对图或树节点进行访问,可以使用并行流提高效率。
eg:
Tree root = // 构建树结构
// 搜索树结构中所有级别为3的节点,并按权重排序
List<Tree> thirdLevelNodes = StreamUtil.iterateHierarchies(root, Tree:getChildren)
.filter(node -> node.getLevel() == 3)
.sorted(Comparator.comparing(Tree::getWeight))
.toList();
T
- 对象类型root
- 根节点discoverer
- 下一层级节点的获取方法public static <T> EasyStream<T> generate(Supplier<T> s)
Supplier
生成
适用场景在一些生成常量流、随机元素等T
- 元素类型s
- 用来生成元素的 Supplier
public static <T> EasyStream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
从重复串行流进行拼接时可能会导致深度调用链甚至抛出 StackOverflowException
T
- 元素类型a
- 第一个流b
- 第二个流public static EasyStream<String> split(CharSequence str, String regex)
str
- 字符串regex
- 正则public <R> EasyStream<R> map(Function<? super T,? extends R> mapper)
R
- 函数执行后返回的类型mapper
- 指定的函数public EasyStream<T> wrap(Stream<T> stream)
stream
- 流public int sum(ToIntFunction<? super T> mapper)
mapper
- 将对象转换为int的 Function
public long sum(ToLongFunction<? super T> mapper)
mapper
- 将对象转换为long的 Function
public double sum(ToDoubleFunction<? super T> mapper)
mapper
- 将对象转换为double的 Function
public <R extends Number> BigDecimal sum(Function<? super T,R> mapper)
Number
类型的总和R
- 数字mapper
- 将对象转换为Number
的 Function
BigDecimal
, 如果流为空, 返回 BigDecimal.ZERO
public Opt<BigDecimal> avg(Function<? super T,BigDecimal> mapper)
BigDecimal
类型的平均值 并以四舍五入的方式保留2位精度mapper
- 将对象转换为BigDecimal
的 Function
Opt.empty()
public Opt<BigDecimal> avg(Function<? super T,BigDecimal> mapper, int scale)
BigDecimal
类型的平均值 并以四舍五入的方式保留小数点后scale位mapper
- 将对象转换为BigDecimal
的 Function
scale
- 保留精度Opt.empty()
public Opt<BigDecimal> avg(Function<? super T,BigDecimal> mapper, int scale, RoundingMode roundingMode)
BigDecimal
类型的平均值mapper
- 将对象转换为BigDecimal
的 Function
scale
- 保留精度roundingMode
- 舍入模式Opt.empty()
public OptionalDouble avg(ToIntFunction<? super T> mapper)
mapper
- 将对象转换为int 的 Function
OptionalDouble
如果流的长度为0 那么会返回OptionalDouble.empty()
public OptionalDouble avg(ToDoubleFunction<? super T> mapper)
mapper
- 将对象转换为double 的 Function
OptionalDouble
如果流的长度为0 那么会返回OptionalDouble.empty()
public OptionalDouble avg(ToLongFunction<? super T> mapper)
mapper
- 将对象转换为long 的 Function
OptionalDouble
如果流的长度为0 那么会返回OptionalDouble.empty()
Copyright © 2025. All rights reserved.