fix comment

This commit is contained in:
Looly 2025-02-06 11:40:42 +08:00
parent 0542866c49
commit c9d1548438
2 changed files with 104 additions and 104 deletions

View File

@ -472,132 +472,132 @@ public class CollectorUtil {
} }
/** /**
* 将一个Collection<T>两个属性分流至两个ArrayList,并使用Pair收集 * 将一个{@code Collection<T>}两个属性分流至两个ArrayList,并使用Pair收集
* *
* @param lMapper 左属性收集方法 * @param lMapper 左属性收集方法
* @param rMapper 右属性收集方法 * @param rMapper 右属性收集方法
* @param <T> 元素类型 * @param <T> 元素类型
* @param <L> 左属性类型 * @param <L> 左属性类型
* @param <R> 右属性类型 * @param <R> 右属性类型
* @return Pair<List<L>,List<R>> Pair收集的两个List * @return {@code Pair<List<L>,List<R>>} Pair收集的两个List
* @author Tanglt * @author Tanglt
*/ */
public static <T, L, R> Collector<T, Pair<List<L>,List<R>>, Pair<List<L>,List<R>>> toPairList(Function<T, L> lMapper, public static <T, L, R> Collector<T, Pair<List<L>, List<R>>, Pair<List<L>, List<R>>> toPairList(final Function<T, L> lMapper,
Function<T, R> rMapper) { final Function<T, R> rMapper) {
return toPairCollection(lMapper,rMapper,ArrayList::new,ArrayList::new); return toPairCollection(lMapper, rMapper, ArrayList::new, ArrayList::new);
} }
/** /**
* 将一个Collection<T>两个属性分流至两个Collection,并使用Pair收集需要指定Collection类型 * 将一个{@code Collection<T>}两个属性分流至两个Collection,并使用Pair收集需要指定Collection类型
* *
* @param lMapper 左属性收集方法 * @param lMapper 左属性收集方法
* @param rMapper 右属性收集方法 * @param rMapper 右属性收集方法
* @param newCollectionL 左属性Collection创建方法 * @param newCollectionL 左属性Collection创建方法
* @param newCollectionR 右属性Collection创建方法 * @param newCollectionR 右属性Collection创建方法
* @param <T> 元素类型 * @param <T> 元素类型
* @param <L> 左属性类型 * @param <L> 左属性类型
* @param <R> 右属性类型 * @param <R> 右属性类型
* @param <LC> 左分流Collection类型 * @param <LC> 左分流Collection类型
* @param <RC> 右分流Collection类型 * @param <RC> 右分流Collection类型
* @return Pair<C<L>,C<R>> Pair收集的两个List * @return {@code Pair<C<L>,C<R>>} Pair收集的两个List
* @author Tanglt * @author Tanglt
*/ */
public static <T, L, R,LC extends Collection<L>,RC extends Collection<R>> public static <T, L, R, LC extends Collection<L>, RC extends Collection<R>>
Collector<T, Pair<LC, RC>, Pair<LC, RC>> toPairCollection(Function<T, L> lMapper, Collector<T, Pair<LC, RC>, Pair<LC, RC>> toPairCollection(final Function<T, L> lMapper,
Function<T, R> rMapper, final Function<T, R> rMapper,
Supplier<LC> newCollectionL, final Supplier<LC> newCollectionL,
Supplier<RC> newCollectionR) { final Supplier<RC> newCollectionR) {
return new SimpleCollector<>(() -> Pair.of(newCollectionL.get(), newCollectionR.get()), return new SimpleCollector<>(() -> Pair.of(newCollectionL.get(), newCollectionR.get()),
(listPair, element) -> { (listPair, element) -> {
L lValue = lMapper.apply(element); final L lValue = lMapper.apply(element);
if (lValue != null) { if (lValue != null) {
listPair.getLeft().add(lValue); listPair.getLeft().add(lValue);
} }
R rValue = rMapper.apply(element); final R rValue = rMapper.apply(element);
if (rValue != null) { if (rValue != null) {
listPair.getRight().add(rValue); listPair.getRight().add(rValue);
} }
}, },
(listPair1, listPair2) -> { (listPair1, listPair2) -> {
listPair1.getLeft().addAll(listPair2.getLeft()); listPair1.getLeft().addAll(listPair2.getLeft());
listPair1.getRight().addAll(listPair2.getRight()); listPair1.getRight().addAll(listPair2.getRight());
return listPair1; return listPair1;
}, },
CH_ID); CH_ID);
} }
/** /**
* 将一个Collection<T>三个属性分流至三个ArrayList,并使用Triple收集 * 将一个{@code Collection<T>}三个属性分流至三个ArrayList,并使用Triple收集
* *
* @param lMapper 左属性收集方法 * @param lMapper 左属性收集方法
* @param mMapper 中属性收集方法 * @param mMapper 中属性收集方法
* @param rMapper 右属性收集方法 * @param rMapper 右属性收集方法
* @param <T> 元素类型 * @param <T> 元素类型
* @param <L> 左属性类型 * @param <L> 左属性类型
* @param <M> 中属性类型 * @param <M> 中属性类型
* @param <R> 右属性类型 * @param <R> 右属性类型
* @return Triple<List<L>,List<M>,List<R>> Triple收集的三个List * @return {@code Triple<List<L>,List<M>,List<R>>} Triple收集的三个List
* @author Tanglt * @author Tanglt
*/ */
public static <T, L, M, R> public static <T, L, M, R>
Collector<T, Triple<List<L>, List<M>, List<R>>, Triple<List<L>, List<M>, List<R>>> toTripleList(Function<T, L> lMapper, Collector<T, Triple<List<L>, List<M>, List<R>>, Triple<List<L>, List<M>, List<R>>> toTripleList(final Function<T, L> lMapper,
Function<T, M> mMapper, final Function<T, M> mMapper,
Function<T, R> rMapper) { final Function<T, R> rMapper) {
return toTripleCollection(lMapper, mMapper, rMapper, ArrayList::new, ArrayList::new, ArrayList::new); return toTripleCollection(lMapper, mMapper, rMapper, ArrayList::new, ArrayList::new, ArrayList::new);
} }
/** /**
* 将一个Collection<T>两个属性分流至两个Collection,并使用Pair收集需要指定Collection类型 * 将一个{@code Collection<T>}两个属性分流至两个Collection,并使用Pair收集需要指定Collection类型
* *
* @param lMapper 左属性收集方法 * @param lMapper 左属性收集方法
* @param mMapper 中属性收集方法 * @param mMapper 中属性收集方法
* @param rMapper 右属性收集方法 * @param rMapper 右属性收集方法
* @param newCollectionL 左属性Collection创建方法 * @param newCollectionL 左属性Collection创建方法
* @param newCollectionM 中属性Collection创建方法 * @param newCollectionM 中属性Collection创建方法
* @param newCollectionR 右属性Collection创建方法 * @param newCollectionR 右属性Collection创建方法
* @param <T> 元素类型 * @param <T> 元素类型
* @param <L> 左属性类型 * @param <L> 左属性类型
* @param <M> 中属性类型 * @param <M> 中属性类型
* @param <R> 右属性类型 * @param <R> 右属性类型
* @param <LC> 左分流Collection类型 * @param <LC> 左分流Collection类型
* @param <MC> 中分流Collection类型 * @param <MC> 中分流Collection类型
* @param <RC> 右分流Collection类型 * @param <RC> 右分流Collection类型
* @return Triple<LC<L>,MC<M>,RC<R>> Triple收集的三个List * @return {@code Triple<LC<L>,MC<M>,RC<R>>} Triple收集的三个List
* @author Tanglt * @author Tanglt
*/ */
public static <T, L, M,R, public static <T, L, M, R,
LC extends Collection<L>, LC extends Collection<L>,
MC extends Collection<M>, MC extends Collection<M>,
RC extends Collection<R>> RC extends Collection<R>>
Collector<T, Triple<LC,MC,RC>,Triple<LC,MC,RC>> toTripleCollection(Function<T, L> lMapper, Collector<T, Triple<LC, MC, RC>, Triple<LC, MC, RC>> toTripleCollection(final Function<T, L> lMapper,
Function<T, M> mMapper, final Function<T, M> mMapper,
Function<T, R> rMapper, final Function<T, R> rMapper,
Supplier<LC> newCollectionL, final Supplier<LC> newCollectionL,
Supplier<MC> newCollectionM, final Supplier<MC> newCollectionM,
Supplier<RC> newCollectionR){ final Supplier<RC> newCollectionR) {
return new SimpleCollector<>( return new SimpleCollector<>(
() -> Triple.of(newCollectionL.get(),newCollectionM.get(), newCollectionR.get()), () -> Triple.of(newCollectionL.get(), newCollectionM.get(), newCollectionR.get()),
(listTriple, element) -> { (listTriple, element) -> {
L lValue = lMapper.apply(element); final L lValue = lMapper.apply(element);
if (lValue != null) { if (lValue != null) {
listTriple.getLeft().add(lValue); listTriple.getLeft().add(lValue);
} }
M mValue = mMapper.apply(element); final M mValue = mMapper.apply(element);
if (mValue != null) { if (mValue != null) {
listTriple.getMiddle().add(mValue); listTriple.getMiddle().add(mValue);
} }
R rValue = rMapper.apply(element); final R rValue = rMapper.apply(element);
if (rValue != null) { if (rValue != null) {
listTriple.getRight().add(rValue); listTriple.getRight().add(rValue);
} }
}, },
(listTriple1, listTriple2) -> { (listTriple1, listTriple2) -> {
listTriple1.getLeft().addAll(listTriple2.getLeft()); listTriple1.getLeft().addAll(listTriple2.getLeft());
listTriple1.getRight().addAll(listTriple2.getRight()); listTriple1.getRight().addAll(listTriple2.getRight());
return listTriple1; return listTriple1;
}, },
CH_ID); CH_ID);
} }
} }

View File

@ -141,13 +141,13 @@ public class CollectorUtilTest {
@Test @Test
public void testToPairList() { public void testToPairList() {
final List<Pair<Integer,String>> list = Arrays.asList(Pair.of(1,"one"), Pair.of(2,"two")); final List<Pair<Integer,String>> list = Arrays.asList(Pair.of(1,"one"), Pair.of(2,"two"));
Pair<List<Integer>, List<String>> pairList = list.stream() final Pair<List<Integer>, List<String>> pairList = list.stream()
.collect(CollectorUtil.toPairList(Pair::getLeft, Pair::getRight)); .collect(CollectorUtil.toPairList(Pair::getLeft, Pair::getRight));
Assertions.assertEquals(pairList.getLeft().size(),list.size()); Assertions.assertEquals(pairList.getLeft().size(),list.size());
Assertions.assertEquals(pairList.getRight().size(),list.size()); Assertions.assertEquals(pairList.getRight().size(),list.size());
Pair<HashSet<Integer>, ArrayList<String>> pairMixed = list.stream() final Pair<HashSet<Integer>, ArrayList<String>> pairMixed = list.stream()
.collect(CollectorUtil.toPairCollection(Pair::getLeft, Pair::getRight, HashSet::new, ArrayList::new)); .collect(CollectorUtil.toPairCollection(Pair::getLeft, Pair::getRight, HashSet::new, ArrayList::new));
Assertions.assertEquals(pairMixed.getLeft().size(),list.size()); Assertions.assertEquals(pairMixed.getLeft().size(),list.size());
@ -160,14 +160,14 @@ public class CollectorUtilTest {
public void testToTripleList() { public void testToTripleList() {
final List<Triple<Integer,Long,String>> list final List<Triple<Integer,Long,String>> list
= Arrays.asList(Triple.of(1,1L,"one"), Triple.of(2,2L,"two")); = Arrays.asList(Triple.of(1,1L,"one"), Triple.of(2,2L,"two"));
Triple<List<Integer>, List<Long>, List<String>> tripleList = list.stream() final Triple<List<Integer>, List<Long>, List<String>> tripleList = list.stream()
.collect(CollectorUtil.toTripleList(Triple::getLeft, Triple::getMiddle, Triple::getRight)); .collect(CollectorUtil.toTripleList(Triple::getLeft, Triple::getMiddle, Triple::getRight));
Assertions.assertEquals(tripleList.getLeft().size(),list.size()); Assertions.assertEquals(tripleList.getLeft().size(),list.size());
Assertions.assertEquals(tripleList.getMiddle().size(),list.size()); Assertions.assertEquals(tripleList.getMiddle().size(),list.size());
Assertions.assertEquals(tripleList.getRight().size(),list.size()); Assertions.assertEquals(tripleList.getRight().size(),list.size());
Triple<HashSet<Integer>, HashSet<Long>, ArrayList<String>> tripleMixed = list.stream() final Triple<HashSet<Integer>, HashSet<Long>, ArrayList<String>> tripleMixed = list.stream()
.collect(CollectorUtil.toTripleCollection(Triple::getLeft, Triple::getMiddle, Triple::getRight, HashSet::new, HashSet::new, ArrayList::new)); .collect(CollectorUtil.toTripleCollection(Triple::getLeft, Triple::getMiddle, Triple::getRight, HashSet::new, HashSet::new, ArrayList::new));
Assertions.assertEquals(tripleMixed.getLeft().size(),list.size()); Assertions.assertEquals(tripleMixed.getLeft().size(),list.size());