From 7cb8492f1871d14027ee07ec1f060075c50f3760 Mon Sep 17 00:00:00 2001 From: kongweiguang Date: Sun, 25 Jun 2023 13:59:30 +0800 Subject: [PATCH] =?UTF-8?q?ListUtil=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=84lis?= =?UTF-8?q?t=E5=86=85=E9=83=A8=E7=9A=84=E5=A4=9A=E4=B8=AAlist=E4=B8=BA?= =?UTF-8?q?=E5=8D=95=E4=B8=AAlist=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/collection/ListUtil.java | 43 +++++++++++++++++++ .../hutool/core/collection/ListUtilTest.java | 37 ++++++++++++---- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/collection/ListUtil.java index ef7b37019..73af2aae7 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/collection/ListUtil.java @@ -27,6 +27,7 @@ import org.dromara.hutool.core.util.ObjUtil; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; +import java.util.function.Function; import java.util.function.Predicate; /** @@ -748,4 +749,46 @@ public class ListUtil { } return resList; } + + /** + * 解构list里面的list为单个list + * + * @param list 传入的list集合 + * @param 返回的元素类型 + * @return 解构后的list集合 + */ + public static List flatList(List list) { + return flatList(list, Function.identity()); + } + + /** + * 解构list里面的list,并可以对每个元素操作 + * + * @param list 传入的list集合 + * @param operation 对每个元素进行操作 + * @param 返回的元素类型 + * @param 最内侧的元素类型 + * @return 解构后的list集合 + */ + @SuppressWarnings("all") + public static List flatList(List list, Function operation) { + List result = new ArrayList<>(); + + if (list == null || list.isEmpty()) { + return result; + } + + if (list.get(0) instanceof List) { + for (List subList : (List>) list) { + result.addAll(flatList(subList, operation)); + } + } else { + for (Object item : list) { + result.add(operation.apply((O) item)); + } + } + + return result; + } + } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/collection/ListUtilTest.java index 563a872d8..a2452fed9 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/collection/ListUtilTest.java @@ -2,6 +2,7 @@ package org.dromara.hutool.core.collection; import lombok.AllArgsConstructor; import lombok.Data; +import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.date.StopWatch; import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.page.PageInfo; @@ -78,7 +79,7 @@ public class ListUtilTest { @Test public void splitAvgNotZero() { - Assertions.assertThrows(IllegalArgumentException.class, ()->{ + Assertions.assertThrows(IllegalArgumentException.class, () -> { // limit不能小于等于0 ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 0); }); @@ -122,7 +123,7 @@ public class ListUtilTest { public void pageTest2() { final List a = ListUtil.ofLinked(1, 2, 3, 4, 5); final int[] d1 = ListUtil.page(a, PageInfo.of(a.size(), 8).setFirstPageNo(0).setPageNo(0)) - .stream().mapToInt(Integer::valueOf).toArray(); + .stream().mapToInt(Integer::valueOf).toArray(); Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, d1); } @@ -170,11 +171,11 @@ public class ListUtilTest { } final List beanList = ListUtil.of( - new TestBean(2, "test2"), - new TestBean(1, "test1"), - new TestBean(5, "test5"), - new TestBean(4, "test4"), - new TestBean(3, "test3") + new TestBean(2, "test2"), + new TestBean(1, "test1"), + new TestBean(5, "test5"), + new TestBean(4, "test4"), + new TestBean(3, "test3") ); final List order = ListUtil.sortByProperty(beanList, "order"); @@ -245,13 +246,13 @@ public class ListUtilTest { } @Test - public void ofCopyOnWriteTest(){ + public void ofCopyOnWriteTest() { final CopyOnWriteArrayList strings = ListUtil.ofCopyOnWrite(ListUtil.of("a", "b")); Assertions.assertEquals(2, strings.size()); } @Test - public void ofCopyOnWriteTest2(){ + public void ofCopyOnWriteTest2() { final CopyOnWriteArrayList strings = ListUtil.ofCopyOnWrite("a", "b"); Assertions.assertEquals(2, strings.size()); } @@ -269,4 +270,22 @@ public class ListUtilTest { ListUtil.reverseNew(list); } + + @Test + public void flatListTest1() { + List>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7"))); + + List objects = ListUtil.flatList(list); + + Assertions.assertArrayEquals(new String[]{"1", "2", "3", "5", "6", "7"}, objects.toArray()); + } + + + @Test + public void flatListTest2() { + List>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7"))); + + List objects = ListUtil.flatList(list, Convert::toInt); + Assertions.assertArrayEquals(new Integer[]{1, 2, 3, 5, 6, 7}, objects.toArray()); + } }