mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
分流收集器收集方式从固定Collection改为自定义收集器,可以自定义如何收集元素
This commit is contained in:
parent
49e9ede07b
commit
3658d9f7ca
@ -489,7 +489,6 @@ public class CollectorUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将一个{@code Collection<T>}两个属性分流至两个Collection,并使用Pair收集。
|
||||
*
|
||||
@ -518,30 +517,30 @@ public class CollectorUtil {
|
||||
() -> Pair.of(lDownstream.supplier().get(), rDownstream.supplier().get()),
|
||||
|
||||
(listPair, element) -> {
|
||||
lDownstream.accumulator().accept(listPair.getLeft(),lMapper.apply(element));
|
||||
rDownstream.accumulator().accept(listPair.getRight(),rMapper.apply(element));
|
||||
lDownstream.accumulator().accept(listPair.getLeft(), lMapper.apply(element));
|
||||
rDownstream.accumulator().accept(listPair.getRight(), rMapper.apply(element));
|
||||
},
|
||||
|
||||
(listPair1, listPair2) ->
|
||||
Pair.of(lDownstream.combiner().apply(listPair1.getLeft(),listPair2.getLeft()),
|
||||
rDownstream.combiner().apply(listPair1.getRight(),listPair2.getRight())),
|
||||
Pair.of(lDownstream.combiner().apply(listPair1.getLeft(), listPair2.getLeft()),
|
||||
rDownstream.combiner().apply(listPair1.getRight(), listPair2.getRight())),
|
||||
|
||||
finisherPair -> {
|
||||
LR finisherLeftValue;
|
||||
final LR finisherLeftValue;
|
||||
if (lDownstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
finisherLeftValue = (LR) finisherPair.getLeft();
|
||||
}else {
|
||||
} else {
|
||||
finisherLeftValue = lDownstream.finisher().apply(finisherPair.getLeft());
|
||||
}
|
||||
|
||||
RR finisherRightValue;
|
||||
final RR finisherRightValue;
|
||||
if (lDownstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
finisherRightValue = (RR) finisherPair.getRight();
|
||||
}else {
|
||||
} else {
|
||||
finisherRightValue = rDownstream.finisher().apply(finisherPair.getRight());
|
||||
}
|
||||
|
||||
return Pair.of(finisherLeftValue,finisherRightValue);
|
||||
return Pair.of(finisherLeftValue, finisherRightValue);
|
||||
},
|
||||
CH_NOID);
|
||||
}
|
||||
@ -568,7 +567,6 @@ public class CollectorUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将一个{@code Collection<T>}两个属性分流至两个Collection,并使用Pair收集。
|
||||
*
|
||||
@ -576,6 +574,7 @@ public class CollectorUtil {
|
||||
* @param mMapper 中元素收集方法
|
||||
* @param rMapper 右元素收集方法
|
||||
* @param lDownstream 左元素下游操作
|
||||
* @param mDownstream 中元素下游操作
|
||||
* @param rDownstream 右元素下游操作
|
||||
* @param <T> 元素类型
|
||||
* @param <LU> 左属性类型
|
||||
@ -592,7 +591,7 @@ public class CollectorUtil {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, LU, LA, LR, MU, MA, MR, RU, RA, RR>
|
||||
Collector<T, ?, Triple<LR,MR, RR>> toTriple(final Function<? super T, ? extends LU> lMapper,
|
||||
Collector<T, ?, Triple<LR, MR, RR>> toTriple(final Function<? super T, ? extends LU> lMapper,
|
||||
final Function<? super T, ? extends MU> mMapper,
|
||||
final Function<? super T, ? extends RU> rMapper,
|
||||
final Collector<? super LU, LA, LR> lDownstream,
|
||||
@ -603,39 +602,39 @@ public class CollectorUtil {
|
||||
() -> Triple.of(lDownstream.supplier().get(), mDownstream.supplier().get(), rDownstream.supplier().get()),
|
||||
|
||||
(listTriple, element) -> {
|
||||
lDownstream.accumulator().accept(listTriple.getLeft(),lMapper.apply(element));
|
||||
mDownstream.accumulator().accept(listTriple.getMiddle(),mMapper.apply(element));
|
||||
rDownstream.accumulator().accept(listTriple.getRight(),rMapper.apply(element));
|
||||
lDownstream.accumulator().accept(listTriple.getLeft(), lMapper.apply(element));
|
||||
mDownstream.accumulator().accept(listTriple.getMiddle(), mMapper.apply(element));
|
||||
rDownstream.accumulator().accept(listTriple.getRight(), rMapper.apply(element));
|
||||
},
|
||||
|
||||
(listTriple1, listTriple2) ->
|
||||
Triple.of(lDownstream.combiner().apply(listTriple1.getLeft(),listTriple2.getLeft()),
|
||||
mDownstream.combiner().apply(listTriple1.getMiddle(),listTriple2.getMiddle()),
|
||||
rDownstream.combiner().apply(listTriple1.getRight(),listTriple2.getRight())),
|
||||
Triple.of(lDownstream.combiner().apply(listTriple1.getLeft(), listTriple2.getLeft()),
|
||||
mDownstream.combiner().apply(listTriple1.getMiddle(), listTriple2.getMiddle()),
|
||||
rDownstream.combiner().apply(listTriple1.getRight(), listTriple2.getRight())),
|
||||
|
||||
finisherTriple -> {
|
||||
LR finisherLeftValue;
|
||||
final LR finisherLeftValue;
|
||||
if (lDownstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
finisherLeftValue = (LR) finisherTriple.getLeft();
|
||||
}else {
|
||||
} else {
|
||||
finisherLeftValue = lDownstream.finisher().apply(finisherTriple.getLeft());
|
||||
}
|
||||
|
||||
MR finisherMiddleValue;
|
||||
final MR finisherMiddleValue;
|
||||
if (mDownstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
finisherMiddleValue = (MR) finisherTriple.getMiddle();
|
||||
}else {
|
||||
} else {
|
||||
finisherMiddleValue = mDownstream.finisher().apply(finisherTriple.getMiddle());
|
||||
}
|
||||
|
||||
RR finisherRightValue;
|
||||
final RR finisherRightValue;
|
||||
if (lDownstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
|
||||
finisherRightValue = (RR) finisherTriple.getRight();
|
||||
}else {
|
||||
} else {
|
||||
finisherRightValue = rDownstream.finisher().apply(finisherTriple.getRight());
|
||||
}
|
||||
|
||||
return Triple.of(finisherLeftValue,finisherMiddleValue,finisherRightValue);
|
||||
return Triple.of(finisherLeftValue, finisherMiddleValue, finisherRightValue);
|
||||
},
|
||||
CH_NOID);
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ public class CollectorUtilTest {
|
||||
Assertions.assertEquals(list.size(),tripleList.getMiddle().size());
|
||||
Assertions.assertEquals(list.size(),tripleList.getRight().size());
|
||||
|
||||
Triple<Integer, List<Long>, String> tripleMixed = list.stream()
|
||||
final Triple<Integer, List<Long>, String> tripleMixed = list.stream()
|
||||
.collect(CollectorUtil.toTriple(Triple::getLeft, Triple::getMiddle, Triple::getRight,
|
||||
Collectors.summingInt(s->s), Collectors.toList(), Collectors.joining()));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user