mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +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,以达到最小长度
|
* 填充List,以达到最小长度
|
||||||
|
*
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @param minLen 最小长度
|
* @param minLen 最小长度
|
||||||
* @param padObj 填充的对象
|
* @param padObj 填充的对象
|
||||||
@ -45,32 +46,26 @@ public class CollUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> void padLeft(List<T> list, int minLen, T padObj) {
|
public static <T> void padLeft(List<T> list, int minLen, T padObj) {
|
||||||
Objects.requireNonNull(list);
|
Objects.requireNonNull(list);
|
||||||
if (isEmpty(list)) {
|
if (list.isEmpty()) {
|
||||||
padRight(list, minLen, padObj);
|
padRight(list, minLen, padObj);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int iterCnt = minLen - list.size();
|
for (int i = list.size(); i < minLen; i++) {
|
||||||
if (iterCnt < 1) {
|
list.add(0, padObj);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
List<T> padList = new ArrayList<>(iterCnt);
|
|
||||||
for (int i = 0; i < iterCnt; i++) {
|
|
||||||
padList.add(padObj);
|
|
||||||
}
|
|
||||||
list.addAll(0, padList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 填充List,以达到最小长度
|
* 填充List,以达到最小长度
|
||||||
|
*
|
||||||
* @param list 列表
|
* @param list 列表
|
||||||
* @param minLen 最小长度
|
* @param minLen 最小长度
|
||||||
* @param padObj 填充的对象
|
* @param padObj 填充的对象
|
||||||
* @param <T> 集合元素类型
|
* @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);
|
Objects.requireNonNull(list);
|
||||||
int iterCnt = minLen - list.size();
|
for (int i = list.size(); i < minLen; i++) {
|
||||||
for (int i = 0; i < iterCnt; i++) {
|
|
||||||
list.add(padObj);
|
list.add(padObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,28 +21,28 @@ public class CollUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPadLeft() {
|
public void testPadLeft() {
|
||||||
List<Integer> srcList = CollUtil.newArrayList();
|
List<String> srcList = CollUtil.newArrayList();
|
||||||
List<Integer> answerList = CollUtil.newArrayList(2, 1);
|
List<String> answerList = CollUtil.newArrayList("a", "b");
|
||||||
CollUtil.padLeft(srcList, 1, 1);
|
CollUtil.padLeft(srcList, 1, "b");
|
||||||
CollUtil.padLeft(srcList, 2, 2);
|
CollUtil.padLeft(srcList, 2, "a");
|
||||||
Assert.assertEquals(srcList, answerList);
|
Assert.assertEquals(srcList, answerList);
|
||||||
|
|
||||||
srcList = CollUtil.newArrayList(1, 2);
|
srcList = CollUtil.newArrayList("a", "b");
|
||||||
answerList = CollUtil.newArrayList(1, 2);
|
answerList = CollUtil.newArrayList("a", "b");
|
||||||
CollUtil.padLeft(srcList, 2, 1);
|
CollUtil.padLeft(srcList, 2, "a");
|
||||||
Assert.assertEquals(srcList, answerList);
|
Assert.assertEquals(srcList, answerList);
|
||||||
|
|
||||||
srcList = CollUtil.newArrayList(3);
|
srcList = CollUtil.newArrayList("c");
|
||||||
answerList = CollUtil.newArrayList(1, 1, 3);
|
answerList = CollUtil.newArrayList("a", "a", "c");
|
||||||
CollUtil.padLeft(srcList, 3, 1);
|
CollUtil.padLeft(srcList, 3, "a");
|
||||||
Assert.assertEquals(srcList, answerList);
|
Assert.assertEquals(srcList, answerList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPadRight() {
|
public void testPadRight() {
|
||||||
List<Integer> srcList = CollUtil.newArrayList(6);
|
List<String> srcList = CollUtil.newArrayList("a");
|
||||||
List<Integer> answerList = CollUtil.newArrayList(6, 3, 3, 3, 3);
|
List<String> answerList = CollUtil.newArrayList("a", "b", "b", "b", "b");
|
||||||
CollUtil.padRight(srcList, 5, 3);
|
CollUtil.padRight(srcList, 5, "b");
|
||||||
Assert.assertEquals(srcList, answerList);
|
Assert.assertEquals(srcList, answerList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user