mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
补充测试用例
This commit is contained in:
parent
835e7bf66d
commit
a079a80908
@ -104,7 +104,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
/**
|
/**
|
||||||
* 返回包含单个元素的串行流
|
* 返回包含单个元素的串行流
|
||||||
*
|
*
|
||||||
* @param t 单个元素
|
* @param t 单个元素
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 包含单个元素的串行流
|
* @return 包含单个元素的串行流
|
||||||
*/
|
*/
|
||||||
@ -116,9 +116,9 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 返回包含指定元素的串行流,若输入数组为{@code null}或空,则返回一个空的串行流
|
* 返回包含指定元素的串行流,若输入数组为{@code null}或空,则返回一个空的串行流
|
||||||
*
|
*
|
||||||
* @param values 指定元素
|
* @param values 指定元素
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 包含指定元素的串行流
|
* @return 包含指定元素的串行流
|
||||||
* 从一个安全数组中创建流
|
* 从一个安全数组中创建流
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
@SuppressWarnings("varargs")
|
@SuppressWarnings("varargs")
|
||||||
@ -130,7 +130,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 通过实现了{@link Iterable}接口的对象创建串行流,若输入对象为{@code null},则返回一个空的串行流
|
* 通过实现了{@link Iterable}接口的对象创建串行流,若输入对象为{@code null},则返回一个空的串行流
|
||||||
*
|
*
|
||||||
* @param iterable 实现了{@link Iterable}接口的对象
|
* @param iterable 实现了{@link Iterable}接口的对象
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 流
|
* @return 流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> of(final Iterable<T> iterable) {
|
public static <T> EasyStream<T> of(final Iterable<T> iterable) {
|
||||||
@ -142,7 +142,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
*
|
*
|
||||||
* @param iterable {@link Iterable}
|
* @param iterable {@link Iterable}
|
||||||
* @param parallel 是否并行
|
* @param parallel 是否并行
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 流
|
* @return 流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> of(final Iterable<T> iterable, final boolean parallel) {
|
public static <T> EasyStream<T> of(final Iterable<T> iterable, final boolean parallel) {
|
||||||
@ -157,7 +157,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 通过传入的{@link Stream}创建流,若输入对象为{@code null},则返回一个空的串行流
|
* 通过传入的{@link Stream}创建流,若输入对象为{@code null},则返回一个空的串行流
|
||||||
*
|
*
|
||||||
* @param stream {@link Stream}
|
* @param stream {@link Stream}
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 流
|
* @return 流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> of(final Stream<T> stream) {
|
public static <T> EasyStream<T> of(final Stream<T> stream) {
|
||||||
@ -173,9 +173,9 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 就可以创建从0开始,每次+1的无限流,使用{@link EasyStream#limit(long)}可以限制元素个数
|
* 就可以创建从0开始,每次+1的无限流,使用{@link EasyStream#limit(long)}可以限制元素个数
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @param seed 初始值
|
* @param seed 初始值
|
||||||
* @param f 用上一个元素作为参数执行并返回一个新的元素
|
* @param f 用上一个元素作为参数执行并返回一个新的元素
|
||||||
* @return 无限有序流
|
* @return 无限有序流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> iterate(final T seed, final UnaryOperator<T> f) {
|
public static <T> EasyStream<T> iterate(final T seed, final UnaryOperator<T> f) {
|
||||||
@ -191,13 +191,14 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 就可以创建包含元素0,1,2的流,使用{@link EasyStream#limit(long)}可以限制元素个数
|
* 就可以创建包含元素0,1,2的流,使用{@link EasyStream#limit(long)}可以限制元素个数
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @param seed 初始值
|
* @param seed 初始值
|
||||||
* @param hasNext 条件值
|
* @param hasNext 条件值
|
||||||
* @param next 用上一个元素作为参数执行并返回一个新的元素
|
* @param next 用上一个元素作为参数执行并返回一个新的元素
|
||||||
* @return 无限有序流
|
* @return 无限有序流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> iterate(final T seed, final Predicate<? super T> hasNext, final UnaryOperator<T> next) {
|
public static <T> EasyStream<T> iterate(final T seed, final Predicate<? super T> hasNext,
|
||||||
|
final UnaryOperator<T> next) {
|
||||||
Objects.requireNonNull(next);
|
Objects.requireNonNull(next);
|
||||||
Objects.requireNonNull(hasNext);
|
Objects.requireNonNull(hasNext);
|
||||||
return new EasyStream<>(StreamUtil.iterate(seed, hasNext, next));
|
return new EasyStream<>(StreamUtil.iterate(seed, hasNext, next));
|
||||||
@ -209,7 +210,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 适用场景在一些生成常量流、随机元素等
|
* 适用场景在一些生成常量流、随机元素等
|
||||||
*
|
*
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @param s 用来生成元素的 {@code Supplier}
|
* @param s 用来生成元素的 {@code Supplier}
|
||||||
* @return 无限串行无序流
|
* @return 无限串行无序流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> generate(final Supplier<T> s) {
|
public static <T> EasyStream<T> generate(final Supplier<T> s) {
|
||||||
@ -224,8 +225,8 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* <p>从重复串行流进行拼接时可能会导致深度调用链甚至抛出 {@code StackOverflowException}</p>
|
* <p>从重复串行流进行拼接时可能会导致深度调用链甚至抛出 {@code StackOverflowException}</p>
|
||||||
*
|
*
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @param a 第一个流
|
* @param a 第一个流
|
||||||
* @param b 第二个流
|
* @param b 第二个流
|
||||||
* @return 拼接两个流之后的流
|
* @return 拼接两个流之后的流
|
||||||
*/
|
*/
|
||||||
public static <T> EasyStream<T> concat(final Stream<? extends T> a, final Stream<? extends T> b) {
|
public static <T> EasyStream<T> concat(final Stream<? extends T> a, final Stream<? extends T> b) {
|
||||||
@ -235,12 +236,13 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
/**
|
/**
|
||||||
* 拆分字符串,转换为串行流
|
* 拆分字符串,转换为串行流
|
||||||
*
|
*
|
||||||
* @param str 字符串
|
* @param str 字符串
|
||||||
* @param regex 正则
|
* @param regex 正则
|
||||||
* @return 拆分后元素组成的流
|
* @return 拆分后元素组成的流
|
||||||
*/
|
*/
|
||||||
public static EasyStream<String> split(final CharSequence str, final String regex) {
|
public static EasyStream<String> split(final CharSequence str, final String regex) {
|
||||||
return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of).orElseGet(EasyStream::empty);
|
return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of)
|
||||||
|
.orElseGet(EasyStream::empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------- Static method end
|
// --------------------------------------------------------------- Static method end
|
||||||
@ -251,7 +253,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* 这是一个无状态中间操作
|
* 这是一个无状态中间操作
|
||||||
*
|
*
|
||||||
* @param mapper 指定的函数
|
* @param mapper 指定的函数
|
||||||
* @param <R> 函数执行后返回的类型
|
* @param <R> 函数执行后返回的类型
|
||||||
* @return 返回叠加操作后的流
|
* @return 返回叠加操作后的流
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -272,8 +274,6 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算int类型总和
|
* 计算int类型总和
|
||||||
*
|
*
|
||||||
@ -312,29 +312,52 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* @return {@link OptionalDouble}
|
* @return {@link OptionalDouble}
|
||||||
*/
|
*/
|
||||||
public BigDecimal sum(final Function<? super T, BigDecimal> mapper) {
|
public BigDecimal sum(final Function<? super T, BigDecimal> mapper) {
|
||||||
return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add);
|
return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算bigDecimal平均值 并以四舍五入的方式保留2位精度
|
||||||
|
*
|
||||||
|
* @param mapper 映射
|
||||||
|
* @return 如果元素的长度为0 那么会返回为空的opt
|
||||||
|
*/
|
||||||
|
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper) {
|
||||||
|
return avg(mapper, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算bigDecimal平均值 并以四舍五入的方式保留scale的进度
|
||||||
|
*
|
||||||
|
* @param mapper 映射
|
||||||
|
* @param scale 精度
|
||||||
|
* @return 如果元素的长度为0 那么会返回为空的opt
|
||||||
|
*/
|
||||||
|
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, int scale) {
|
||||||
|
return avg(mapper, scale, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算bigDecimal平均值
|
* 计算bigDecimal平均值
|
||||||
|
*
|
||||||
* @param mapper 映射
|
* @param mapper 映射
|
||||||
* @param scale 精度
|
* @param scale 精度
|
||||||
* @param roundingMode 舍入模式
|
* @param roundingMode 舍入模式
|
||||||
* @return 如果元素的长度为0 那么会返回为空的opt
|
* @return 如果元素的长度为0 那么会返回为空的opt
|
||||||
*/
|
*/
|
||||||
public Opt<BigDecimal> avg(final Function<? super T,BigDecimal> mapper,int scale,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());
|
List<BigDecimal> bigDecimalList = stream.map(mapper).collect(Collectors.toList());
|
||||||
if (CollUtil.isEmpty(bigDecimalList)){
|
if (CollUtil.isEmpty(bigDecimalList)) {
|
||||||
return Opt.ofNullable(null);
|
return Opt.ofNullable(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add).divide(NumberUtil.toBigDecimal(bigDecimalList.size()),scale, roundingMode));
|
return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||||
|
.divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算int平均值
|
* 计算int平均值
|
||||||
*
|
*
|
||||||
@ -367,7 +390,6 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 建造者
|
* 建造者
|
||||||
*
|
*
|
||||||
@ -381,12 +403,12 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
|||||||
* @param t the element to add
|
* @param t the element to add
|
||||||
* @return {@code this} builder
|
* @return {@code this} builder
|
||||||
* @throws IllegalStateException if the builder has already transitioned to
|
* @throws IllegalStateException if the builder has already transitioned to
|
||||||
* the built state
|
* the built state
|
||||||
* @implSpec The default implementation behaves as if:
|
* @implSpec The default implementation behaves as if:
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* accept(t)
|
* accept(t)
|
||||||
* return this;
|
* return this;
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*/
|
*/
|
||||||
default Builder<T> add(final T t) {
|
default Builder<T> add(final T t) {
|
||||||
accept(t);
|
accept(t);
|
||||||
|
@ -2,8 +2,10 @@ package cn.hutool.core.stream;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import cn.hutool.core.lang.Opt;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.math.NumberUtil;
|
import cn.hutool.core.math.NumberUtil;
|
||||||
|
import java.math.RoundingMode;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -85,10 +87,10 @@ public class EasyStreamTest {
|
|||||||
final List<Integer> orders = Arrays.asList(1, 2, 3, 2);
|
final List<Integer> orders = Arrays.asList(1, 2, 3, 2);
|
||||||
final List<String> list = Arrays.asList("dromara", "guava", "sweet", "hutool");
|
final List<String> list = Arrays.asList("dromara", "guava", "sweet", "hutool");
|
||||||
final Map<Integer, String> map = MapUtil.<Integer, String>builder()
|
final Map<Integer, String> map = MapUtil.<Integer, String>builder()
|
||||||
.put(1, "dromara")
|
.put(1, "dromara")
|
||||||
.put(2, "hutool")
|
.put(2, "hutool")
|
||||||
.put(3, "sweet")
|
.put(3, "sweet")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final Map<Integer, String> toZip = EasyStream.of(orders).toZip(list);
|
final Map<Integer, String> toZip = EasyStream.of(orders).toZip(list);
|
||||||
Assert.assertEquals(map, toZip);
|
Assert.assertEquals(map, toZip);
|
||||||
@ -126,15 +128,15 @@ public class EasyStreamTest {
|
|||||||
final List<Integer> list = Arrays.asList(1, 2, 3);
|
final List<Integer> list = Arrays.asList(1, 2, 3);
|
||||||
final Map<String, List<Integer>> group = EasyStream.of(list).group(String::valueOf);
|
final Map<String, List<Integer>> group = EasyStream.of(list).group(String::valueOf);
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
new HashMap<String, List<Integer>>() {
|
new HashMap<String, List<Integer>>() {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
{
|
{
|
||||||
put("1", singletonList(1));
|
put("1", singletonList(1));
|
||||||
put("2", singletonList(2));
|
put("2", singletonList(2));
|
||||||
put("3", singletonList(3));
|
put("3", singletonList(3));
|
||||||
}
|
}
|
||||||
}, group);
|
}, group);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -144,7 +146,7 @@ public class EasyStreamTest {
|
|||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
||||||
// 并行流时正常
|
// 并行流时正常
|
||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
||||||
EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList());
|
EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -185,7 +187,8 @@ public class EasyStreamTest {
|
|||||||
Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2));
|
Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2));
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count()
|
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true)
|
||||||
|
.distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,28 +224,28 @@ public class EasyStreamTest {
|
|||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
||||||
// 并行流时正常
|
// 并行流时正常
|
||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
||||||
EasyStream.of("dromara", "hutool", "sweet").parallel()
|
EasyStream.of("dromara", "hutool", "sweet").parallel()
|
||||||
.flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList());
|
.flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPeek() {
|
public void testPeek() {
|
||||||
EasyStream.of("one", "two", "three", "four")
|
EasyStream.of("one", "two", "three", "four")
|
||||||
.filter(e -> e.length() == 4)
|
.filter(e -> e.length() == 4)
|
||||||
.peek(e -> Assert.assertEquals("four", e))
|
.peek(e -> Assert.assertEquals("four", e))
|
||||||
.map(String::toUpperCase)
|
.map(String::toUpperCase)
|
||||||
.peek(e -> Assert.assertEquals("FOUR", e))
|
.peek(e -> Assert.assertEquals("FOUR", e))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPeekIdx() {
|
public void testPeekIdx() {
|
||||||
EasyStream.of("one", "two", "three", "four")
|
EasyStream.of("one", "two", "three", "four")
|
||||||
.filter(e -> e.length() == 4)
|
.filter(e -> e.length() == 4)
|
||||||
.peekIdx((e, i) -> Assert.assertEquals("four:0", e + ":" + i))
|
.peekIdx((e, i) -> Assert.assertEquals("four:0", e + ":" + i))
|
||||||
.map(String::toUpperCase)
|
.map(String::toUpperCase)
|
||||||
.peekIdx((e, i) -> Assert.assertEquals("FOUR:0", e + ":" + i))
|
.peekIdx((e, i) -> Assert.assertEquals("FOUR:0", e + ":" + i))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -278,7 +281,7 @@ public class EasyStreamTest {
|
|||||||
Assert.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex);
|
Assert.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex);
|
||||||
// 并行流时正常
|
// 并行流时正常
|
||||||
Assert.assertEquals(Arrays.asList("dromara", "hutool"),
|
Assert.assertEquals(Arrays.asList("dromara", "hutool"),
|
||||||
EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList());
|
EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -384,23 +387,23 @@ public class EasyStreamTest {
|
|||||||
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
||||||
|
|
||||||
final List<String> res1 = EasyStream.of(list)
|
final List<String> res1 = EasyStream.of(list)
|
||||||
// 舍弃 5
|
// 舍弃 5
|
||||||
.takeWhile(e -> e < 5)
|
.takeWhile(e -> e < 5)
|
||||||
// 过滤奇数
|
// 过滤奇数
|
||||||
.filter(e -> (e & 1) == 0)
|
.filter(e -> (e & 1) == 0)
|
||||||
// 反序
|
// 反序
|
||||||
.sorted(Comparator.reverseOrder())
|
.sorted(Comparator.reverseOrder())
|
||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.toList();
|
.toList();
|
||||||
Assert.assertEquals(Arrays.asList("4", "2"), res1);
|
Assert.assertEquals(Arrays.asList("4", "2"), res1);
|
||||||
|
|
||||||
final List<Integer> res2 = EasyStream.iterate(1, i -> i + 1)
|
final List<Integer> res2 = EasyStream.iterate(1, i -> i + 1)
|
||||||
.parallel()
|
.parallel()
|
||||||
.takeWhile(e -> e < 5)
|
.takeWhile(e -> e < 5)
|
||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.map(Integer::valueOf)
|
.map(Integer::valueOf)
|
||||||
.sorted(Comparator.naturalOrder())
|
.sorted(Comparator.naturalOrder())
|
||||||
.toList();
|
.toList();
|
||||||
Assert.assertEquals(Arrays.asList(1, 2, 3, 4), res2);
|
Assert.assertEquals(Arrays.asList(1, 2, 3, 4), res2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,25 +413,25 @@ public class EasyStreamTest {
|
|||||||
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
||||||
|
|
||||||
final List<String> res1 = EasyStream.of(list)
|
final List<String> res1 = EasyStream.of(list)
|
||||||
// 舍弃 5之前的数字
|
// 舍弃 5之前的数字
|
||||||
.dropWhile(e -> e < 5)
|
.dropWhile(e -> e < 5)
|
||||||
// 过滤偶数
|
// 过滤偶数
|
||||||
.filter(e -> (e & 1) == 1)
|
.filter(e -> (e & 1) == 1)
|
||||||
// 反序
|
// 反序
|
||||||
.sorted(Comparator.reverseOrder())
|
.sorted(Comparator.reverseOrder())
|
||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.toList();
|
.toList();
|
||||||
Assert.assertEquals(Arrays.asList("9", "7", "5"), res1);
|
Assert.assertEquals(Arrays.asList("9", "7", "5"), res1);
|
||||||
|
|
||||||
final List<Integer> res2 = EasyStream.of(list)
|
final List<Integer> res2 = EasyStream.of(list)
|
||||||
.parallel()
|
.parallel()
|
||||||
.dropWhile(e -> e < 5)
|
.dropWhile(e -> e < 5)
|
||||||
// 过滤偶数
|
// 过滤偶数
|
||||||
.filter(e -> (e & 1) == 1)
|
.filter(e -> (e & 1) == 1)
|
||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.map(Integer::valueOf)
|
.map(Integer::valueOf)
|
||||||
.sorted(Comparator.naturalOrder())
|
.sorted(Comparator.naturalOrder())
|
||||||
.toList();
|
.toList();
|
||||||
Assert.assertEquals(Arrays.asList(5, 7, 9), res2);
|
Assert.assertEquals(Arrays.asList(5, 7, 9), res2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,6 +443,7 @@ public class EasyStreamTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIntSumAndAvg() {
|
public void testIntSumAndAvg() {
|
||||||
|
//测试int类型的总和
|
||||||
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);
|
||||||
|
|
||||||
@ -447,38 +451,102 @@ public class EasyStreamTest {
|
|||||||
List<Integer> integerList = new ArrayList<>();
|
List<Integer> integerList = new ArrayList<>();
|
||||||
int emptySum = EasyStream.of(integerList).sum(Integer::intValue);
|
int emptySum = EasyStream.of(integerList).sum(Integer::intValue);
|
||||||
Assert.assertEquals(emptySum, 0);
|
Assert.assertEquals(emptySum, 0);
|
||||||
|
|
||||||
|
//测试平均值
|
||||||
|
OptionalDouble avg = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).avg(Integer::intValue);
|
||||||
|
Assert.assertEquals(avg.getAsDouble(), 5.5, 2);
|
||||||
|
|
||||||
|
//测试元素为空
|
||||||
|
OptionalDouble emptyAvg = EasyStream.of(integerList).avg(Integer::intValue);
|
||||||
|
Assert.assertFalse(emptyAvg.isPresent());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoubleSumAndAvg() {
|
public void testDoubleSumAndAvg() {
|
||||||
|
//测试double类型的sum
|
||||||
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);
|
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);
|
Assert.assertEquals(doubleSum, 59.6, 2);
|
||||||
|
|
||||||
|
//测试double类型的sum 无元素double
|
||||||
List<Double> doubleList = new ArrayList<>();
|
List<Double> doubleList = new ArrayList<>();
|
||||||
double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue);
|
double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue);
|
||||||
Assert.assertEquals(emptySum, 0.0,2);
|
Assert.assertEquals(emptySum, 0.0, 2);
|
||||||
|
|
||||||
|
//测试double类型的avg
|
||||||
|
OptionalDouble doubleAvg = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
|
||||||
|
.avg(Double::doubleValue);
|
||||||
|
Assert.assertEquals(doubleAvg.getAsDouble(), 5.96, 2);
|
||||||
|
|
||||||
|
//测试double类型的 空元素的avg
|
||||||
|
OptionalDouble emptyDoubleAvg = EasyStream.of(doubleList).avg(Double::doubleValue);
|
||||||
|
Assert.assertFalse(emptyDoubleAvg.isPresent());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLongSumAndAvg() {
|
public void testLongSumAndAvg() {
|
||||||
|
//测试long类型的sum
|
||||||
long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue);
|
long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue);
|
||||||
Assert.assertEquals(sum, 55);
|
Assert.assertEquals(sum, 55);
|
||||||
|
|
||||||
|
//测试long类型的空元素 sum
|
||||||
List<Long> longList = new ArrayList<>();
|
List<Long> longList = new ArrayList<>();
|
||||||
double emptySum = EasyStream.of(longList).sum(Long::longValue);
|
long emptySum = EasyStream.of(longList).sum(Long::longValue);
|
||||||
Assert.assertEquals(emptySum, 0L);
|
Assert.assertEquals(emptySum, 0L);
|
||||||
|
|
||||||
|
//测试long类型的avg
|
||||||
|
OptionalDouble doubleAvg = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).avg(Long::longValue);
|
||||||
|
Assert.assertEquals(doubleAvg.getAsDouble(), 5.5, 2);
|
||||||
|
|
||||||
|
//测试long类型的avg 空元素
|
||||||
|
OptionalDouble emptyDoubleAvg = EasyStream.of(longList).avg(Long::longValue);
|
||||||
|
Assert.assertFalse(emptyDoubleAvg.isPresent());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBigDecimalSumAndAvg() {
|
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());
|
//测试bigDecimal的sum
|
||||||
Assert.assertEquals(sum, 59.6);
|
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, NumberUtil.toBigDecimal(59.6));
|
||||||
|
|
||||||
List<BigDecimal> bigDecimalList = new ArrayList<>();
|
//测试bigDecimal的sum 空元素
|
||||||
BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity());
|
List<BigDecimal> bigDecimalEmptyList = new ArrayList<>();
|
||||||
|
BigDecimal emptySum = EasyStream.of(bigDecimalEmptyList).sum(Function.identity());
|
||||||
Assert.assertEquals(emptySum, BigDecimal.ZERO);
|
Assert.assertEquals(emptySum, BigDecimal.ZERO);
|
||||||
|
|
||||||
|
//测试bigDecimal的avg
|
||||||
|
Opt<BigDecimal> bigDecimalAvgFullParam = 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))
|
||||||
|
.avg(Function.identity(), 2, RoundingMode.HALF_UP);
|
||||||
|
Assert.assertEquals(bigDecimalAvgFullParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||||
|
|
||||||
|
Opt<BigDecimal> bigDecimalAvgOneParam = 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))
|
||||||
|
.avg(Function.identity());
|
||||||
|
Assert.assertEquals(bigDecimalAvgOneParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||||
|
|
||||||
|
Opt<BigDecimal> bigDecimalAvgTwoParam = 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))
|
||||||
|
.avg(Function.identity(), 2);
|
||||||
|
Assert.assertEquals(bigDecimalAvgTwoParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||||
|
|
||||||
|
//测试bigDecimal的avg 空元素
|
||||||
|
Opt<BigDecimal> emptyBigDecimalAvg = EasyStream.of(bigDecimalEmptyList)
|
||||||
|
.avg(Function.identity(), 2, RoundingMode.HALF_UP);
|
||||||
|
Assert.assertFalse(emptyBigDecimalAvg.isPresent());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user