From 33e95b23c1b8bb822f57fe4f83dc49debc06eaf6 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 20 Jun 2022 19:49:07 +0800 Subject: [PATCH] =?UTF-8?q?BeanPath=E5=9C=A8=E7=A9=BA=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E6=97=B6=E9=BB=98=E8=AE=A4=E5=8A=A0=E5=85=A5map=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=B9=E6=8D=AE=E4=B8=8B=E6=A0=87=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=B5=8B=E5=80=BCList=20or=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../java/cn/hutool/core/bean/BeanPath.java | 61 ++++++++++----- .../java/cn/hutool/core/bean/BeanUtil.java | 12 ++- .../cn/hutool/core/collection/ListUtil.java | 41 ++++++++++ .../cn/hutool/core/bean/BeanPathTest.java | 64 +++++++++------ .../hutool/core/collection/ListUtilTest.java | 78 ++++++++++++------- 6 files changed, 183 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48633aaa1..ced7dc6bf 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 【core 】 BlockPolicy增加线程池关闭后的逻辑(pr#660@Gitee) * 【core 】 Ipv4Util增加ipv4ToLong重载(pr#661@Gitee) * 【core 】 LocalDateTimeUtil.parse改为blank检查(issue#I5CZJ9@Gitee) +* 【core 】 BeanPath在空元素时默认加入map,修改根据下标类型赋值List or map(issue#2362@Github) * ### 🐞Bug修复 * 【extra 】 修复createExtractor中抛出异常后流未关闭问题(pr#2384@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanPath.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanPath.java index d9e67c99a..324bc19c7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/bean/BeanPath.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanPath.java @@ -6,6 +6,7 @@ import cn.hutool.core.convert.Convert; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharUtil; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import java.io.Serializable; @@ -22,7 +23,7 @@ import java.util.Map; *
  • .表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值
  • *
  • []表达式,可以获取集合等对象中对应index的值
  • * - * + *

    * 表达式栗子: * *

    @@ -36,11 +37,13 @@ import java.util.Map;
      * @author Looly
      * @since 4.0.6
      */
    -public class BeanPath implements Serializable{
    +public class BeanPath implements Serializable {
     	private static final long serialVersionUID = 1L;
     
    -	/** 表达式边界符号数组 */
    -	private static final char[] EXP_CHARS = { CharUtil.DOT, CharUtil.BRACKET_START, CharUtil.BRACKET_END };
    +	/**
    +	 * 表达式边界符号数组
    +	 */
    +	private static final char[] EXP_CHARS = {CharUtil.DOT, CharUtil.BRACKET_START, CharUtil.BRACKET_END};
     
     	private boolean isStartWith = false;
     	protected List patternParts;
    @@ -53,7 +56,7 @@ public class BeanPath implements Serializable{
     	 * 
  • .表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值
  • *
  • []表达式,可以获取集合等对象中对应index的值
  • * - * + *

    * 表达式栗子: * *

    @@ -85,7 +88,7 @@ public class BeanPath implements Serializable{
     	 *
     	 * @return 表达式分段列表
     	 */
    -	public List getPatternParts(){
    +	public List getPatternParts() {
     		return this.patternParts;
     	}
     
    @@ -109,11 +112,11 @@ public class BeanPath implements Serializable{
     	 * 2. 如果为数组,如果下标不大于数组长度,则替换原有值,否则追加值
     	 * 
    * - * @param bean Bean、Map或List + * @param bean Bean、Map或List * @param value 值 */ public void set(final Object bean, final Object value) { - set(bean, this.patternParts, value); + set(bean, this.patternParts, lastIsNumber(this.patternParts), value); } @Override @@ -122,6 +125,7 @@ public class BeanPath implements Serializable{ } //region Private Methods + /** * 设置表达式指定位置(或filed对应)的值
    * 若表达式指向一个List则设置其坐标对应位置的值,若指向Map则put对应key的值,Bean则设置字段的值
    @@ -132,26 +136,47 @@ public class BeanPath implements Serializable{ * 2. 如果为数组,如果下标不大于数组长度,则替换原有值,否则追加值 *
    * - * @param bean Bean、Map或List + * @param bean Bean、Map或List * @param patternParts 表达式块列表 - * @param value 值 + * @param value 值 */ - private void set(final Object bean, final List patternParts, final Object value) { - Object subBean = get(patternParts, bean, true); - if(null == subBean) { - set(bean, patternParts.subList(0, patternParts.size() - 1), new HashMap<>()); + private void set(Object bean, List patternParts, boolean nextNumberPart, Object value) { + Object subBean = this.get(patternParts, bean, true); + if (null == subBean) { + final List parentParts = getParentParts(patternParts); + this.set(bean, parentParts, lastIsNumber(parentParts), nextNumberPart ? new ArrayList<>() : new HashMap<>()); //set中有可能做过转换,因此此处重新获取bean - subBean = get(patternParts, bean, true); + subBean = this.get(patternParts, bean, true); } BeanUtil.setFieldValue(subBean, patternParts.get(patternParts.size() - 1), value); } + /** + * 判断path列表中末尾的标记是否为数字 + * + * @param patternParts path列表 + * @return 是否为数字 + */ + private static boolean lastIsNumber(List patternParts) { + return NumberUtil.isInteger(patternParts.get(patternParts.size() - 1)); + } + + /** + * 获取父级路径列表 + * + * @param patternParts 路径列表 + * @return 父级路径列表 + */ + private static List getParentParts(List patternParts) { + return patternParts.subList(0, patternParts.size() - 1); + } + /** * 获取Bean中对应表达式的值 * * @param patternParts 表达式分段列表 - * @param bean Bean对象或Map或List等 - * @param ignoreLast 是否忽略最后一个值,忽略最后一个值则用于set,否则用于read + * @param bean Bean对象或Map或List等 + * @param ignoreLast 是否忽略最后一个值,忽略最后一个值则用于set,否则用于read * @return 值,如果对应值不存在,则返回null */ private Object get(final List patternParts, final Object bean, final boolean ignoreLast) { @@ -247,7 +272,7 @@ public class BeanPath implements Serializable{ continue; } - if('\'' == c){ + if ('\'' == c) { // 结束 isInWrap = (false == isInWrap); continue; diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java index f4034c2f1..8815c6ef5 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java @@ -4,6 +4,7 @@ import cn.hutool.core.bean.copier.BeanCopier; import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.bean.copier.ValueProvider; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.lang.Editor; import cn.hutool.core.map.CaseInsensitiveMap; @@ -23,7 +24,14 @@ import java.beans.PropertyEditor; import java.beans.PropertyEditorManager; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -309,7 +317,7 @@ public class BeanUtil { if (bean instanceof Map) { ((Map) bean).put(fieldNameOrIndex, value); } else if (bean instanceof List) { - CollUtil.setOrAppend((List) bean, Convert.toInt(fieldNameOrIndex), value); + ListUtil.setOrPadding((List) bean, Convert.toInt(fieldNameOrIndex), value); } else if (ArrayUtil.isArray(bean)) { ArrayUtil.setOrAppend(bean, Convert.toInt(fieldNameOrIndex), value); } else { diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index b0d8bfe12..b6dedc5b2 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -2,6 +2,7 @@ package cn.hutool.core.collection; import cn.hutool.core.comparator.PinyinComparator; import cn.hutool.core.comparator.PropertyComparator; +import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Matcher; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; @@ -384,6 +385,7 @@ public class ListUtil { * @since 4.1.2 */ public static List setOrAppend(List list, int index, T element) { + Assert.notNull(list, "List must be not null !"); if (index < list.size()) { list.set(index, element); } else { @@ -392,6 +394,45 @@ public class ListUtil { return list; } + /** + * 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code null}直到到达index后,设置值 + * + * @param 元素类型 + * @param list List列表 + * @param index 位置 + * @param element 新元素 + * @return 原List + * @since 5。8.4 + */ + public static List setOrPadding(List list, int index, T element) { + return setOrPadding(list, index, element, null); + } + + /** + * 在指定位置设置元素。当index小于List的长度时,替换指定位置的值,否则追加{@code paddingElement}直到到达index后,设置值 + * + * @param 元素类型 + * @param list List列表 + * @param index 位置 + * @param element 新元素 + * @param paddingElement 填充的值 + * @return 原List + * @since 5。8.4 + */ + public static List setOrPadding(List list, int index, T element, T paddingElement) { + Assert.notNull(list, "List must be not null !"); + final int size = list.size(); + if (index < size) { + list.set(index, element); + } else { + for (int i = size; i < index; i++) { + list.add(paddingElement); + } + list.add(element); + } + return list; + } + /** * 截取集合的部分 * diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanPathTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanPathTest.java index 4b323f48c..e0cd8731b 100644 --- a/hutool-core/src/test/java/cn/hutool/core/bean/BeanPathTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanPathTest.java @@ -1,22 +1,20 @@ package cn.hutool.core.bean; +import cn.hutool.core.lang.test.bean.ExamInfoDict; +import cn.hutool.core.lang.test.bean.UserInfoDict; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import cn.hutool.core.lang.test.bean.ExamInfoDict; -import cn.hutool.core.lang.test.bean.UserInfoDict; - /** * {@link BeanPath} 单元测试 * * @author looly - * */ public class BeanPathTest { @@ -25,28 +23,28 @@ public class BeanPathTest { @Before public void init() { // ------------------------------------------------- 考试信息列表 - ExamInfoDict examInfoDict = new ExamInfoDict(); + final ExamInfoDict examInfoDict = new ExamInfoDict(); examInfoDict.setId(1); examInfoDict.setExamType(0); examInfoDict.setAnswerIs(1); - ExamInfoDict examInfoDict1 = new ExamInfoDict(); + final ExamInfoDict examInfoDict1 = new ExamInfoDict(); examInfoDict1.setId(2); examInfoDict1.setExamType(0); examInfoDict1.setAnswerIs(0); - ExamInfoDict examInfoDict2 = new ExamInfoDict(); + final ExamInfoDict examInfoDict2 = new ExamInfoDict(); examInfoDict2.setId(3); examInfoDict2.setExamType(1); examInfoDict2.setAnswerIs(0); - List examInfoDicts = new ArrayList<>(); + final List examInfoDicts = new ArrayList<>(); examInfoDicts.add(examInfoDict); examInfoDicts.add(examInfoDict1); examInfoDicts.add(examInfoDict2); // ------------------------------------------------- 用户信息 - UserInfoDict userInfoDict = new UserInfoDict(); + final UserInfoDict userInfoDict = new UserInfoDict(); userInfoDict.setId(1); userInfoDict.setPhotoPath("yx.mm.com"); userInfoDict.setRealName("张三"); @@ -59,7 +57,7 @@ public class BeanPathTest { @Test public void beanPathTest1() { - BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id"); + final BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id"); Assert.assertEquals("userInfo", pattern.patternParts.get(0)); Assert.assertEquals("examInfoDict", pattern.patternParts.get(1)); Assert.assertEquals("0", pattern.patternParts.get(2)); @@ -69,7 +67,7 @@ public class BeanPathTest { @Test public void beanPathTest2() { - BeanPath pattern = new BeanPath("[userInfo][examInfoDict][0][id]"); + final BeanPath pattern = new BeanPath("[userInfo][examInfoDict][0][id]"); Assert.assertEquals("userInfo", pattern.patternParts.get(0)); Assert.assertEquals("examInfoDict", pattern.patternParts.get(1)); Assert.assertEquals("0", pattern.patternParts.get(2)); @@ -78,7 +76,7 @@ public class BeanPathTest { @Test public void beanPathTest3() { - BeanPath pattern = new BeanPath("['userInfo']['examInfoDict'][0]['id']"); + final BeanPath pattern = new BeanPath("['userInfo']['examInfoDict'][0]['id']"); Assert.assertEquals("userInfo", pattern.patternParts.get(0)); Assert.assertEquals("examInfoDict", pattern.patternParts.get(1)); Assert.assertEquals("0", pattern.patternParts.get(2)); @@ -87,25 +85,43 @@ public class BeanPathTest { @Test public void getTest() { - BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id"); - Object result = pattern.get(tempMap); + final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id"); + final Object result = pattern.get(tempMap); Assert.assertEquals(1, result); } @Test public void setTest() { - BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id"); + final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id"); pattern.set(tempMap, 2); - Object result = pattern.get(tempMap); + final Object result = pattern.get(tempMap); Assert.assertEquals(2, result); } @Test - public void getMapTest () { - BeanPath pattern = BeanPath.create("userInfo[id, photoPath]"); - @SuppressWarnings("unchecked") - Map result = (Map)pattern.get(tempMap); + public void getMapTest() { + final BeanPath pattern = BeanPath.create("userInfo[id, photoPath]"); + @SuppressWarnings("unchecked") final Map result = (Map) pattern.get(tempMap); Assert.assertEquals(1, result.get("id")); Assert.assertEquals("yx.mm.com", result.get("photoPath")); } + + @Test + public void issue2362Test() { + final Map map = new HashMap<>(); + + BeanPath beanPath = BeanPath.create("list[0].name"); + beanPath.set(map, "张三"); + Assert.assertEquals("{list=[{name=张三}]}", map.toString()); + + map.clear(); + beanPath = BeanPath.create("list[1].name"); + beanPath.set(map, "张三"); + Assert.assertEquals("{list=[null, {name=张三}]}", map.toString()); + + map.clear(); + beanPath = BeanPath.create("list[0].1.name"); + beanPath.set(map, "张三"); + Assert.assertEquals("{list=[[null, {name=张三}]]}", map.toString()); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index 937fb5917..64e8a5b2b 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java @@ -38,20 +38,20 @@ public class ListUtilTest { @Test @Ignore public void splitBenchTest() { - List list = new ArrayList<>(); + final List list = new ArrayList<>(); CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test"); - int size = RandomUtil.randomInt(10, 1000); + final int size = RandomUtil.randomInt(10, 1000); Console.log("\nlist size: {}", list.size()); Console.log("partition size: {}\n", size); - StopWatch stopWatch = new StopWatch(); + final StopWatch stopWatch = new StopWatch(); stopWatch.start("CollUtil#split"); - List> CollSplitResult = CollUtil.split(list, size); + final List> CollSplitResult = CollUtil.split(list, size); stopWatch.stop(); stopWatch.start("ListUtil#split"); - List> ListSplitResult = ListUtil.split(list, size); + final List> ListSplitResult = ListUtil.split(list, size); stopWatch.stop(); Assert.assertEquals(CollSplitResult, ListSplitResult); @@ -87,7 +87,7 @@ public class ListUtilTest { @Test public void editTest() { - List a = ListUtil.toLinkedList("1", "2", "3"); + final List a = ListUtil.toLinkedList("1", "2", "3"); final List filter = (List) CollUtil.edit(a, str -> "edit" + str); Assert.assertEquals("edit1", filter.get(0)); Assert.assertEquals("edit2", filter.get(1)); @@ -96,7 +96,7 @@ public class ListUtilTest { @Test public void indexOfAll() { - List a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1"); + final List a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1"); final int[] indexArray = ListUtil.indexOfAll(a, "2"::equals); Assert.assertArrayEquals(new int[]{1,5}, indexArray); final int[] indexArray2 = ListUtil.indexOfAll(a, "1"::equals); @@ -105,14 +105,14 @@ public class ListUtilTest { @Test public void pageTest() { - List a = ListUtil.toLinkedList(1, 2, 3,4,5); + final List a = ListUtil.toLinkedList(1, 2, 3,4,5); PageUtil.setFirstPageNo(1); - int[] a_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] a1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] a2 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] a3 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] a4 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] a_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] a1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] a2 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] a3 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] a4 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray(); Assert.assertArrayEquals(new int[]{1,2},a_1); Assert.assertArrayEquals(new int[]{1,2},a1); Assert.assertArrayEquals(new int[]{3,4},a2); @@ -121,11 +121,11 @@ public class ListUtilTest { PageUtil.setFirstPageNo(2); - int[] b_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] b1 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] b2 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] b3 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] b4 = ListUtil.page(5,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] b_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] b1 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] b2 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] b3 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] b4 = ListUtil.page(5,2,a).stream().mapToInt(Integer::valueOf).toArray(); Assert.assertArrayEquals(new int[]{1,2},b_1); Assert.assertArrayEquals(new int[]{1,2},b1); Assert.assertArrayEquals(new int[]{3,4},b2); @@ -133,11 +133,11 @@ public class ListUtilTest { Assert.assertArrayEquals(new int[]{},b4); PageUtil.setFirstPageNo(0); - int[] c_1 = ListUtil.page(-1,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] c1 = ListUtil.page(0,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] c2 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] c3 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); - int[] c4 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] c_1 = ListUtil.page(-1,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] c1 = ListUtil.page(0,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] c2 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] c3 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] c4 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray(); Assert.assertArrayEquals(new int[]{1,2},c_1); Assert.assertArrayEquals(new int[]{1,2},c1); Assert.assertArrayEquals(new int[]{3,4},c2); @@ -146,11 +146,11 @@ public class ListUtilTest { PageUtil.setFirstPageNo(1); - int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray(); + final int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray(); Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1); // page with consumer - List> pageListData = new ArrayList<>(); + final List> pageListData = new ArrayList<>(); ListUtil.page(a, 2, pageListData::add); Assert.assertArrayEquals(new int[]{1, 2}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray()); Assert.assertArrayEquals(new int[]{3, 4}, pageListData.get(1).stream().mapToInt(Integer::valueOf).toArray()); @@ -210,20 +210,20 @@ public class ListUtilTest { @Test public void swapIndex() { - List list = Arrays.asList(7, 2, 8, 9); + final List list = Arrays.asList(7, 2, 8, 9); ListUtil.swapTo(list, 8, 1); Assert.assertEquals(8, (int) list.get(1)); } @Test public void swapElement() { - Map map1 = new HashMap<>(); + final Map map1 = new HashMap<>(); map1.put("1", "张三"); - Map map2 = new HashMap<>(); + final Map map2 = new HashMap<>(); map2.put("2", "李四"); - Map map3 = new HashMap<>(); + final Map map3 = new HashMap<>(); map3.put("3", "王五"); - List> list = Arrays.asList(map1, map2, map3); + final List> list = Arrays.asList(map1, map2, map3); ListUtil.swapElement(list, map2, map3); Map map = list.get(2); Assert.assertEquals("李四", map.get("2")); @@ -232,4 +232,22 @@ public class ListUtilTest { map = list.get(0); Assert.assertEquals("李四", map.get("2")); } + + @Test + public void setOrPaddingNullTest(){ + final List list = new ArrayList<>(); + list.add("1"); + + // 替换原值 + ListUtil.setOrPadding(list, 0, "a"); + Assert.assertEquals("[a]", list.toString()); + + //append值 + ListUtil.setOrPadding(list, 1, "a"); + Assert.assertEquals("[a, a]", list.toString()); + + // padding null 后加入值 + ListUtil.setOrPadding(list, 3, "a"); + Assert.assertEquals(4, list.size()); + } }