mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge remote-tracking branch 'origin/v6-dev' into v6-dev
# Conflicts: # hutool-core/src/main/java/cn/hutool/core/stream/EasyStream.java
This commit is contained in:
commit
5556701c91
@ -206,11 +206,11 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
|
||||
/**
|
||||
* 返回无限串行无序流
|
||||
* 其中每一个元素都由给定的{@code Supplier}生成
|
||||
* 其中每一个元素都由给定的{@link Supplier}生成
|
||||
* 适用场景在一些生成常量流、随机元素等
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param s 用来生成元素的 {@code Supplier}
|
||||
* @param s 用来生成元素的 {@link Supplier}
|
||||
* @return 无限串行无序流
|
||||
*/
|
||||
public static <T> EasyStream<T> generate(final Supplier<T> s) {
|
||||
@ -275,52 +275,53 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
|
||||
|
||||
/**
|
||||
* 计算int类型的总和
|
||||
* 计算int类型总和
|
||||
*
|
||||
* @param mapper 将对象转换为int的 {@link Function}
|
||||
* @return int 总和
|
||||
* @param mapper 映射
|
||||
* @return int
|
||||
*/
|
||||
public int sum(final ToIntFunction<? super T> mapper) {
|
||||
public int sum(ToIntFunction<? super T> mapper) {
|
||||
return stream.mapToInt(mapper).sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算long类型的总和
|
||||
* 计算long类型总和
|
||||
*
|
||||
* @param mapper 将对象转换为long的 {@link Function}
|
||||
* @return long 总和
|
||||
* @param mapper 映射
|
||||
* @return long
|
||||
*/
|
||||
public long sum(final ToLongFunction<? super T> mapper) {
|
||||
public long sum(ToLongFunction<? super T> mapper) {
|
||||
return stream.mapToLong(mapper).sum();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算double类型的总和
|
||||
* 计算double总和
|
||||
*
|
||||
* @param mapper 将对象转换为double的 {@link Function}
|
||||
* @return double 总和
|
||||
* @param mapper 映射器
|
||||
* @return double
|
||||
*/
|
||||
public double sum(final ToDoubleFunction<? super T> mapper) {
|
||||
public double sum(ToDoubleFunction<? super T> mapper) {
|
||||
return stream.mapToDouble(mapper).sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算 {@link Number} 类型的总和
|
||||
* 计算number的总和
|
||||
*
|
||||
* @param mapper 将对象转换为{@link Number} 的 {@link Function}
|
||||
* @return {@link BigDecimal} , 如果流为空, 返回 {@link BigDecimal#ZERO}
|
||||
* @param mapper 映射
|
||||
* @param <R> 映射后的类型
|
||||
* @return {@link BigDecimal}
|
||||
*/
|
||||
public <R extends Number> BigDecimal sum(final Function<? super T, R> mapper) {
|
||||
return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add, NumberUtil::add);
|
||||
return stream.map(mapper).reduce(BigDecimal.ZERO, NumberUtil::add,NumberUtil::add);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算 {@link BigDecimal} 类型的平均值 并以四舍五入的方式保留2位精度
|
||||
* 计算bigDecimal平均值 并以四舍五入的方式保留2位精度
|
||||
*
|
||||
* @param mapper 将对象转换为{@link BigDecimal}的 {@link Function}
|
||||
* @return {@link Opt}<{@link BigDecimal}>; 如果流的长度为0, 返回 {@link Opt#empty()}
|
||||
* @param mapper 映射
|
||||
* @return 计算结果 如果元素的长度为0 那么会返回为空的opt
|
||||
*/
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper) {
|
||||
return avg(mapper, 2);
|
||||
@ -328,54 +329,53 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
|
||||
|
||||
/**
|
||||
* {@link BigDecimal} 类型的平均值 并以四舍五入的方式保留小数点后scale位
|
||||
* 计算bigDecimal平均值 并以四舍五入的方式保留scale的进度
|
||||
*
|
||||
* @param mapper 将对象转换为{@link BigDecimal} 的 {@link Function}
|
||||
* @param scale 保留精度
|
||||
* @return {@link Opt}<{@link BigDecimal}> 如果流的长度为0, 返回 {@link Opt#empty()}
|
||||
* @param mapper 映射
|
||||
* @param scale 精度
|
||||
* @return 计算结果 如果元素的长度为0 那么会返回为空的opt
|
||||
*/
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, final int scale) {
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, int scale) {
|
||||
return avg(mapper, scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算 {@link BigDecimal} 类型的平均值
|
||||
* 计算bigDecimal平均值
|
||||
*
|
||||
* @param mapper 将对象转换为{@link BigDecimal} 的 {@link Function}
|
||||
* @param scale 保留精度
|
||||
* @param mapper 映射
|
||||
* @param scale 精度
|
||||
* @param roundingMode 舍入模式
|
||||
* @return {@link Opt}<{@link BigDecimal}> 如果元素的长度为0 那么会返回 {@link Opt#empty()}
|
||||
* @return 计算结果 如果元素的长度为0 那么会返回为空的opt
|
||||
*/
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, final int scale,
|
||||
final RoundingMode roundingMode) {
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, int scale, RoundingMode roundingMode) {
|
||||
//元素列表
|
||||
List<BigDecimal> bigDecimalList = stream.map(mapper).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(bigDecimalList)) {
|
||||
return Opt.empty();
|
||||
return Opt.ofNullable(null);
|
||||
}
|
||||
|
||||
return Opt.ofNullable(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||
return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||
.divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算int类型的平均值
|
||||
* 计算int平均值
|
||||
*
|
||||
* @param mapper 将对象转换为int 的 {@link Function}
|
||||
* @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()}
|
||||
* @param mapper 映射器
|
||||
* @return {@link OptionalDouble}
|
||||
*/
|
||||
public OptionalDouble avg(final ToIntFunction<? super T> mapper) {
|
||||
public OptionalDouble avg(ToIntFunction<? super T> mapper) {
|
||||
return stream.mapToInt(mapper).average();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算double类型的平均值
|
||||
* 计算double平均值
|
||||
*
|
||||
* @param mapper 将对象转换为double 的 {@link Function}
|
||||
* @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()}
|
||||
* @param mapper 映射
|
||||
* @return {@link OptionalDouble}
|
||||
*/
|
||||
public OptionalDouble avg(final ToDoubleFunction<? super T> mapper) {
|
||||
public OptionalDouble avg(ToDoubleFunction<? super T> mapper) {
|
||||
return stream.mapToDouble(mapper).average();
|
||||
}
|
||||
|
||||
@ -383,10 +383,10 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
/**
|
||||
* 计算double平均值
|
||||
*
|
||||
* @param mapper 将对象转换为long 的 {@link Function}
|
||||
* @return {@link OptionalDouble} 如果流的长度为0 那么会返回{@link OptionalDouble#empty()}
|
||||
* @param mapper 映射
|
||||
* @return {@link OptionalDouble}
|
||||
*/
|
||||
public OptionalDouble avg(final ToLongFunction<? super T> mapper) {
|
||||
public OptionalDouble avg(ToLongFunction<? super T> mapper) {
|
||||
return stream.mapToLong(mapper).average();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user