mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
feat: utility method for padding list
This commit is contained in:
parent
d9f862ce67
commit
78729283e8
@ -38,6 +38,7 @@ public class CollUtil {
|
||||
|
||||
/**
|
||||
* 填充List,以达到最小长度
|
||||
*
|
||||
* @param list 列表
|
||||
* @param minLen 最小长度
|
||||
* @param padObj 填充的对象
|
||||
@ -45,32 +46,26 @@ public class CollUtil {
|
||||
*/
|
||||
public static <T> void padLeft(List<T> 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<T> 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 <T> 集合元素类型
|
||||
*/
|
||||
public static <T> void padRight(List<T> list, int minLen, T padObj) {
|
||||
public static <T> void padRight(Collection<T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -21,28 +21,28 @@ public class CollUtilTest {
|
||||
|
||||
@Test
|
||||
public void testPadLeft() {
|
||||
List<Integer> srcList = CollUtil.newArrayList();
|
||||
List<Integer> answerList = CollUtil.newArrayList(2, 1);
|
||||
CollUtil.padLeft(srcList, 1, 1);
|
||||
CollUtil.padLeft(srcList, 2, 2);
|
||||
List<String> srcList = CollUtil.newArrayList();
|
||||
List<String> 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<Integer> srcList = CollUtil.newArrayList(6);
|
||||
List<Integer> answerList = CollUtil.newArrayList(6, 3, 3, 3, 3);
|
||||
CollUtil.padRight(srcList, 5, 3);
|
||||
List<String> srcList = CollUtil.newArrayList("a");
|
||||
List<String> answerList = CollUtil.newArrayList("a", "b", "b", "b", "b");
|
||||
CollUtil.padRight(srcList, 5, "b");
|
||||
Assert.assertEquals(srcList, answerList);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user