mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加针对 bigDecimal类型的avg方法
This commit is contained in:
parent
b1e9f263bc
commit
835e7bf66d
@ -1,13 +1,19 @@
|
|||||||
package cn.hutool.core.stream;
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.lang.Opt;
|
import cn.hutool.core.lang.Opt;
|
||||||
|
import cn.hutool.core.math.NumberUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.OptionalDouble;
|
import java.util.OptionalDouble;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
@ -299,6 +305,34 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
return stream.mapToDouble(mapper).sum();
|
return stream.mapToDouble(mapper).sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算decimal的总和
|
||||||
|
*
|
||||||
|
* @param mapper 映射
|
||||||
|
* @return {@link OptionalDouble}
|
||||||
|
*/
|
||||||
|
public BigDecimal sum(final Function<? super T, BigDecimal> mapper) {
|
||||||
|
return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算bigDecimal平均值
|
||||||
|
* @param mapper 映射
|
||||||
|
* @param scale 精度
|
||||||
|
* @param roundingMode 舍入模式
|
||||||
|
* @return 如果元素的长度为0 那么会返回为空的opt
|
||||||
|
*/
|
||||||
|
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.ofNullable(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add).divide(NumberUtil.toBigDecimal(bigDecimalList.size()),scale, roundingMode));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -333,6 +367,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 建造者
|
* 建造者
|
||||||
*
|
*
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package cn.hutool.core.stream;
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.math.NumberUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@ -435,11 +439,46 @@ public class EasyStreamTest {
|
|||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIntSum() {
|
public void testIntSumAndAvg() {
|
||||||
int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue);
|
int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue);
|
||||||
Assert.assertEquals(sum, 55);
|
Assert.assertEquals(sum, 55);
|
||||||
double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue);
|
|
||||||
Assert.assertEquals(doubleSum,59.6);
|
//测试为空
|
||||||
|
List<Integer> integerList = new ArrayList<>();
|
||||||
|
int emptySum = EasyStream.of(integerList).sum(Integer::intValue);
|
||||||
|
Assert.assertEquals(emptySum, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDoubleSumAndAvg() {
|
||||||
|
double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue);
|
||||||
|
Assert.assertEquals(doubleSum, 59.6,2);
|
||||||
|
|
||||||
|
List<Double> doubleList = new ArrayList<>();
|
||||||
|
double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue);
|
||||||
|
Assert.assertEquals(emptySum, 0.0,2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLongSumAndAvg() {
|
||||||
|
long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue);
|
||||||
|
Assert.assertEquals(sum, 55);
|
||||||
|
|
||||||
|
List<Long> longList = new ArrayList<>();
|
||||||
|
double emptySum = EasyStream.of(longList).sum(Long::longValue);
|
||||||
|
Assert.assertEquals(emptySum, 0L);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBigDecimalSumAndAvg() {
|
||||||
|
BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity());
|
||||||
|
Assert.assertEquals(sum, 59.6);
|
||||||
|
|
||||||
|
List<BigDecimal> bigDecimalList = new ArrayList<>();
|
||||||
|
BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity());
|
||||||
|
Assert.assertEquals(emptySum, BigDecimal.ZERO);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user