添加支持对流进行转换的收集器

This commit is contained in:
huangchengxing 2022-08-30 15:04:30 +08:00
parent 9e20dbb7a0
commit 4893a5e9aa
2 changed files with 107 additions and 46 deletions

View File

@ -4,15 +4,7 @@ import cn.hutool.core.lang.Opt;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import java.util.Collections; import java.util.*;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
import java.util.function.Function; import java.util.function.Function;
@ -251,5 +243,57 @@ public class CollectorUtil {
); );
} }
/**
* 将流转为{@link EasyStream}
*
* @param <T> 输入元素类型
* @return 收集器
*/
public static <T> Collector<T, ?, EasyStream<T>> toEasyStream() {
return transform(ArrayList::new, EasyStream::of);
}
/**
* 收集元素将其转为指定{@link Collection}集合后再对该集合进行转换并最终返回转换后的结果
* 返回的收集器的效果等同于
* <pre>{@code
* Collection<T> coll = Stream.of(a, b, c, d)
* .collect(Collectors.toCollection(collFactory));
* R result = mapper.apply(coll);
* }</pre>
*
* @param collFactory 中间收集输入元素的集合的创建方法
* @param mapper 最终将元素集合映射为返回值的方法
* @param <R> 返回值类型
* @param <T> 输入元素类型
* @param <C> 中间收集输入元素的集合类型
* @return 收集器
*/
public static <T, R, C extends Collection<T>> Collector<T, C, R> transform(
Supplier<C> collFactory, Function<C, R> mapper) {
Objects.requireNonNull(collFactory);
Objects.requireNonNull(mapper);
return new SimpleCollector<>(
collFactory, C::add, (l1, l2) -> { l1.addAll(l2); return l1; }, mapper, CH_NOID
);
}
/**
* 收集元素将其转为{@link ArrayList}集合后再对该集合进行转换并最终返回转换后的结果
* 返回的收集器的效果等同于
* <pre>{@code
* List<T> coll = Stream.of(a, b, c, d)
* .collect(Collectors.toList());
* R result = mapper.apply(coll);
* }</pre>
*
* @param mapper 最终将元素集合映射为返回值的方法
* @param <R> 返回值类型
* @param <T> 输入元素类型
* @return 收集器
*/
public static <T, R> Collector<T, List<T>, R> transform(Function<List<T>, R> mapper) {
return transform(ArrayList::new, mapper);
}
} }

View File

@ -4,11 +4,9 @@ import cn.hutool.core.map.MapUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* CollectorUtilTest * CollectorUtilTest
@ -34,4 +32,23 @@ public class CollectorUtilTest {
.put("特拉叙马霍斯", Arrays.asList(3, 1, 2)).build(), .put("特拉叙马霍斯", Arrays.asList(3, 1, 2)).build(),
nameScoresMap); nameScoresMap);
} }
@Test
public void testTransform() {
Stream<Integer> stream = Stream.of(1, 2, 3, 4)
.collect(CollectorUtil.transform(EasyStream::of));
Assert.assertEquals(EasyStream.class, stream.getClass());
stream = Stream.of(1, 2, 3, 4)
.collect(CollectorUtil.transform(HashSet::new, EasyStream::of));
Assert.assertEquals(EasyStream.class, stream.getClass());
}
@Test
public void testToEasyStream() {
Stream<Integer> stream =Stream.of(1, 2, 3, 4)
.collect(CollectorUtil.toEasyStream());
Assert.assertEquals(EasyStream.class, stream.getClass());
}
} }