diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index b9709f1cf..4d444b2c4 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -38,6 +38,7 @@ public class CollUtil { /** * 填充List,以达到最小长度 + * * @param list 列表 * @param minLen 最小长度 * @param padObj 填充的对象 @@ -45,32 +46,26 @@ public class CollUtil { */ public static void padLeft(List list, int minLen, T padObj) { Objects.requireNonNull(list); - if (isEmpty(list)) { + if (list.isEmpty()) { padRight(list, minLen, padObj); return; } - int iterCnt = minLen - list.size(); - if (iterCnt < 1) { - return; + for (int i = list.size(); i < minLen; i++) { + list.add(0, padObj); } - List padList = new ArrayList<>(iterCnt); - for (int i = 0; i < iterCnt; i++) { - padList.add(padObj); - } - list.addAll(0, padList); } /** * 填充List,以达到最小长度 + * * @param list 列表 * @param minLen 最小长度 * @param padObj 填充的对象 * @param 集合元素类型 */ - public static void padRight(List list, int minLen, T padObj) { + public static void padRight(Collection list, int minLen, T padObj) { Objects.requireNonNull(list); - int iterCnt = minLen - list.size(); - for (int i = 0; i < iterCnt; i++) { + for (int i = list.size(); i < minLen; i++) { list.add(padObj); } } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index d18a52ea7..12d2a95c2 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -21,28 +21,28 @@ public class CollUtilTest { @Test public void testPadLeft() { - List srcList = CollUtil.newArrayList(); - List answerList = CollUtil.newArrayList(2, 1); - CollUtil.padLeft(srcList, 1, 1); - CollUtil.padLeft(srcList, 2, 2); + List srcList = CollUtil.newArrayList(); + List answerList = CollUtil.newArrayList("a", "b"); + CollUtil.padLeft(srcList, 1, "b"); + CollUtil.padLeft(srcList, 2, "a"); Assert.assertEquals(srcList, answerList); - srcList = CollUtil.newArrayList(1, 2); - answerList = CollUtil.newArrayList(1, 2); - CollUtil.padLeft(srcList, 2, 1); + srcList = CollUtil.newArrayList("a", "b"); + answerList = CollUtil.newArrayList("a", "b"); + CollUtil.padLeft(srcList, 2, "a"); Assert.assertEquals(srcList, answerList); - srcList = CollUtil.newArrayList(3); - answerList = CollUtil.newArrayList(1, 1, 3); - CollUtil.padLeft(srcList, 3, 1); + srcList = CollUtil.newArrayList("c"); + answerList = CollUtil.newArrayList("a", "a", "c"); + CollUtil.padLeft(srcList, 3, "a"); Assert.assertEquals(srcList, answerList); } @Test public void testPadRight() { - List srcList = CollUtil.newArrayList(6); - List answerList = CollUtil.newArrayList(6, 3, 3, 3, 3); - CollUtil.padRight(srcList, 5, 3); + List srcList = CollUtil.newArrayList("a"); + List answerList = CollUtil.newArrayList("a", "b", "b", "b", "b"); + CollUtil.padRight(srcList, 5, "b"); Assert.assertEquals(srcList, answerList); }