mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
:trollface:
This commit is contained in:
parent
62c80a2184
commit
0f3c658746
@ -495,4 +495,25 @@ public class CollectorUtil {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>过滤</p >
|
||||||
|
*
|
||||||
|
* @param predicate 断言
|
||||||
|
* @param downstream 下游操作
|
||||||
|
* @param <T> 元素类型
|
||||||
|
* @param <A> 中间类型
|
||||||
|
* @param <R> 结束类型
|
||||||
|
* @return 一个用于过滤元素的 {@link java.util.stream.Collector}
|
||||||
|
* @author TanShengYuan
|
||||||
|
*/
|
||||||
|
public static <T, A, R>
|
||||||
|
Collector<T, ?, R> filtering(final Predicate<? super T> predicate,
|
||||||
|
final Collector<? super T, A, R> downstream) {
|
||||||
|
final BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
|
||||||
|
return new SimpleCollector<>(downstream.supplier(),
|
||||||
|
(r, t) -> Opt.of(t).filter(predicate).ifPresent(e -> downstreamAccumulator.accept(r, e)),
|
||||||
|
downstream.combiner(), downstream.finisher(),
|
||||||
|
downstream.characteristics());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,50 +20,62 @@ public class CollectorUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void reduceListMapTest() {
|
public void reduceListMapTest() {
|
||||||
final Set<Map<String, Integer>> nameScoreMapList = StreamUtil.of(
|
final Set<Map<String, Integer>> nameScoreMapList = StreamUtil.of(
|
||||||
// 集合内的第一个map,包含两个key value
|
// 集合内的第一个map,包含两个key value
|
||||||
MapUtil.builder("苏格拉底", 1).put("特拉叙马霍斯", 3).build(),
|
MapUtil.builder("苏格拉底", 1).put("特拉叙马霍斯", 3).build(),
|
||||||
MapUtil.of("苏格拉底", 2),
|
MapUtil.of("苏格拉底", 2),
|
||||||
MapUtil.of("特拉叙马霍斯", 1),
|
MapUtil.of("特拉叙马霍斯", 1),
|
||||||
MapUtil.of("特拉叙马霍斯", 2)
|
MapUtil.of("特拉叙马霍斯", 2)
|
||||||
).collect(Collectors.toSet());
|
).collect(Collectors.toSet());
|
||||||
// 执行聚合
|
// 执行聚合
|
||||||
final Map<String, List<Integer>> nameScoresMap = nameScoreMapList.stream().collect(CollectorUtil.reduceListMap());
|
final Map<String, List<Integer>> nameScoresMap = nameScoreMapList.stream().collect(CollectorUtil.reduceListMap());
|
||||||
|
|
||||||
Assert.assertEquals(MapUtil.builder("苏格拉底", Arrays.asList(1, 2))
|
Assert.assertEquals(MapUtil.builder("苏格拉底", Arrays.asList(1, 2))
|
||||||
.put("特拉叙马霍斯", Arrays.asList(3, 1, 2)).build(),
|
.put("特拉叙马霍斯", Arrays.asList(3, 1, 2)).build(),
|
||||||
nameScoresMap);
|
nameScoresMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTransform() {
|
public void testTransform() {
|
||||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4)
|
Stream<Integer> stream = Stream.of(1, 2, 3, 4)
|
||||||
.collect(CollectorUtil.transform(EasyStream::of));
|
.collect(CollectorUtil.transform(EasyStream::of));
|
||||||
Assert.assertEquals(EasyStream.class, stream.getClass());
|
Assert.assertEquals(EasyStream.class, stream.getClass());
|
||||||
|
|
||||||
stream = Stream.of(1, 2, 3, 4)
|
stream = Stream.of(1, 2, 3, 4)
|
||||||
.collect(CollectorUtil.transform(HashSet::new, EasyStream::of));
|
.collect(CollectorUtil.transform(HashSet::new, EasyStream::of));
|
||||||
Assert.assertEquals(EasyStream.class, stream.getClass());
|
Assert.assertEquals(EasyStream.class, stream.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToEasyStream() {
|
public void testToEasyStream() {
|
||||||
final Stream<Integer> stream =Stream.of(1, 2, 3, 4)
|
final Stream<Integer> stream = Stream.of(1, 2, 3, 4)
|
||||||
.collect(CollectorUtil.toEasyStream());
|
.collect(CollectorUtil.toEasyStream());
|
||||||
Assert.assertEquals(EasyStream.class, stream.getClass());
|
Assert.assertEquals(EasyStream.class, stream.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToEntryStream() {
|
public void testToEntryStream() {
|
||||||
final Map<String, Integer> map = Stream.of(1, 2, 3, 4, 5)
|
final Map<String, Integer> map = Stream.of(1, 2, 3, 4, 5)
|
||||||
// 转为EntryStream
|
// 转为EntryStream
|
||||||
.collect(CollectorUtil.toEntryStream(Function.identity(), String::valueOf))
|
.collect(CollectorUtil.toEntryStream(Function.identity(), String::valueOf))
|
||||||
// 过滤偶数
|
// 过滤偶数
|
||||||
.filterByKey(k -> (k & 1) == 1)
|
.filterByKey(k -> (k & 1) == 1)
|
||||||
.inverse()
|
.inverse()
|
||||||
.toMap();
|
.toMap();
|
||||||
Assert.assertEquals((Integer)1, map.get("1"));
|
Assert.assertEquals((Integer) 1, map.get("1"));
|
||||||
Assert.assertEquals((Integer)3, map.get("3"));
|
Assert.assertEquals((Integer) 3, map.get("3"));
|
||||||
Assert.assertEquals((Integer)5, map.get("5"));
|
Assert.assertEquals((Integer) 5, map.get("5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFiltering() {
|
||||||
|
final Map<Integer, Long> map = Stream.of(1, 2, 3)
|
||||||
|
.collect(Collectors.groupingBy(Function.identity(),
|
||||||
|
CollectorUtil.filtering(i -> i > 1, Collectors.counting())
|
||||||
|
));
|
||||||
|
Assert.assertEquals(MapUtil.builder()
|
||||||
|
.put(1, 0L)
|
||||||
|
.put(2, 1L)
|
||||||
|
.put(3, 1L)
|
||||||
|
.build(), map);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user