From 6ac537d6c336e656ee4b4e2059f3bcf9a45d8e71 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 5 Jul 2022 22:26:27 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 1 + .../hutool/core/collection/CollUtilTest.java | 270 +++++++++--------- 2 files changed, 136 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f917d22ee..cc69df71a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * 【core 】 新增CollectorUtil.reduceListMap()(pr#676@Gitee) * ### 🐞Bug修复 +* 【core 】 修复CollUtil里面关于可变参数传null造成的crash问题(pr#2428@Github) ------------------------------------------------------------------------------------------------------------- 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 fcf2505d0..19d0f1114 100755 --- 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,7 +21,7 @@ public class CollUtilTest { @Test public void testPredicateContains() { - ArrayList list = CollUtil.newArrayList("bbbbb", "aaaaa", "ccccc"); + final ArrayList list = CollUtil.newArrayList("bbbbb", "aaaaa", "ccccc"); Assert.assertTrue(CollUtil.contains(list, s -> s.startsWith("a"))); Assert.assertFalse(CollUtil.contains(list, s -> s.startsWith("d"))); } @@ -29,8 +29,8 @@ public class CollUtilTest { @Test public void testRemoveWithAddIf() { ArrayList list = CollUtil.newArrayList(1, 2, 3); - ArrayList exceptRemovedList = CollUtil.newArrayList(2, 3); - ArrayList exceptResultList = CollUtil.newArrayList(1); + final ArrayList exceptRemovedList = CollUtil.newArrayList(2, 3); + final ArrayList exceptResultList = CollUtil.newArrayList(1); List resultList = CollUtil.removeWithAddIf(list, ele -> 1 == ele); Assert.assertEquals(list, exceptRemovedList); @@ -64,8 +64,8 @@ public class CollUtilTest { @Test public void testPadRight() { - List srcList = CollUtil.newArrayList("a"); - List answerList = CollUtil.newArrayList("a", "b", "b", "b", "b"); + final List srcList = CollUtil.newArrayList("a"); + final List answerList = CollUtil.newArrayList("a", "b", "b", "b", "b"); CollUtil.padRight(srcList, 5, "b"); Assert.assertEquals(srcList, answerList); } @@ -77,22 +77,22 @@ public class CollUtilTest { @Test public void newHashSetTest() { - Set set = CollUtil.newHashSet((String[]) null); + final Set set = CollUtil.newHashSet((String[]) null); Assert.assertNotNull(set); } @Test public void valuesOfKeysTest() { - Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23); - Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); + final Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23); + final Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); final String[] keys = v1.keySet().toArray(new String[0]); - ArrayList v1s = CollUtil.valuesOfKeys(v1, keys); + final ArrayList v1s = CollUtil.valuesOfKeys(v1, keys); Assert.assertTrue(v1s.contains(12)); Assert.assertTrue(v1s.contains(23)); Assert.assertTrue(v1s.contains("张三")); - ArrayList v2s = CollUtil.valuesOfKeys(v2, keys); + final ArrayList v2s = CollUtil.valuesOfKeys(v2, keys); Assert.assertTrue(v2s.contains(15)); Assert.assertTrue(v2s.contains(13)); Assert.assertTrue(v2s.contains("李四")); @@ -100,47 +100,47 @@ public class CollUtilTest { @Test public void unionTest() { - ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); - ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); + final ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); - Collection union = CollUtil.union(list1, list2); + final Collection union = CollUtil.union(list1, list2); Assert.assertEquals(3, CollUtil.count(union, "b"::equals)); } @Test public void intersectionTest() { - ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); - ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); + final ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); - Collection intersection = CollUtil.intersection(list1, list2); + final Collection intersection = CollUtil.intersection(list1, list2); Assert.assertEquals(2, CollUtil.count(intersection, "b"::equals)); } @Test public void intersectionDistinctTest() { - ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); - ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); - ArrayList list3 = CollUtil.newArrayList(); + final ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d"); + final ArrayList list3 = CollUtil.newArrayList(); - Collection intersectionDistinct = CollUtil.intersectionDistinct(list1, list2); + final Collection intersectionDistinct = CollUtil.intersectionDistinct(list1, list2); Assert.assertEquals(CollUtil.newLinkedHashSet("a", "b", "c", "d"), intersectionDistinct); - Collection intersectionDistinct2 = CollUtil.intersectionDistinct(list1, list2, list3); + final Collection intersectionDistinct2 = CollUtil.intersectionDistinct(list1, list2, list3); Assert.assertTrue(intersectionDistinct2.isEmpty()); } @Test public void disjunctionTest() { - ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); - ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); + final ArrayList list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); - Collection disjunction = CollUtil.disjunction(list1, list2); + final Collection disjunction = CollUtil.disjunction(list1, list2); Assert.assertTrue(disjunction.contains("b")); Assert.assertTrue(disjunction.contains("x2")); Assert.assertTrue(disjunction.contains("x")); - Collection disjunction2 = CollUtil.disjunction(list2, list1); + final Collection disjunction2 = CollUtil.disjunction(list2, list1); Assert.assertTrue(disjunction2.contains("b")); Assert.assertTrue(disjunction2.contains("x2")); Assert.assertTrue(disjunction2.contains("x")); @@ -149,29 +149,29 @@ public class CollUtilTest { @Test public void disjunctionTest2() { // 任意一个集合为空,差集为另一个集合 - ArrayList list1 = CollUtil.newArrayList(); - ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); + final ArrayList list1 = CollUtil.newArrayList(); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); - Collection disjunction = CollUtil.disjunction(list1, list2); + final Collection disjunction = CollUtil.disjunction(list1, list2); Assert.assertEquals(list2, disjunction); - Collection disjunction2 = CollUtil.disjunction(list2, list1); + final Collection disjunction2 = CollUtil.disjunction(list2, list1); Assert.assertEquals(list2, disjunction2); } @Test public void disjunctionTest3() { // 无交集下返回共同的元素 - ArrayList list1 = CollUtil.newArrayList("1", "2", "3"); - ArrayList list2 = CollUtil.newArrayList("a", "b", "c"); + final ArrayList list1 = CollUtil.newArrayList("1", "2", "3"); + final ArrayList list2 = CollUtil.newArrayList("a", "b", "c"); - Collection disjunction = CollUtil.disjunction(list1, list2); + final Collection disjunction = CollUtil.disjunction(list1, list2); Assert.assertTrue(disjunction.contains("1")); Assert.assertTrue(disjunction.contains("2")); Assert.assertTrue(disjunction.contains("3")); Assert.assertTrue(disjunction.contains("a")); Assert.assertTrue(disjunction.contains("b")); Assert.assertTrue(disjunction.contains("c")); - Collection disjunction2 = CollUtil.disjunction(list2, list1); + final Collection disjunction2 = CollUtil.disjunction(list2, list1); Assert.assertTrue(disjunction2.contains("1")); Assert.assertTrue(disjunction2.contains("2")); Assert.assertTrue(disjunction2.contains("3")); @@ -182,8 +182,8 @@ public class CollUtilTest { @Test public void subtractTest() { - List list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); - List list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); + final List list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x"); + final List list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); final Collection subtract = CollUtil.subtract(list1, list2); Assert.assertEquals(1, subtract.size()); Assert.assertEquals("x", subtract.iterator().next()); @@ -191,55 +191,55 @@ public class CollUtilTest { @Test public void subtractSetTest() { - HashMap map1 = MapUtil.newHashMap(); - HashMap map2 = MapUtil.newHashMap(); + final HashMap map1 = MapUtil.newHashMap(); + final HashMap map2 = MapUtil.newHashMap(); map1.put("1", "v1"); map1.put("2", "v2"); map2.put("2", "v2"); - Collection r2 = CollUtil.subtract(map1.keySet(), map2.keySet()); + final Collection r2 = CollUtil.subtract(map1.keySet(), map2.keySet()); Assert.assertEquals("[1]", r2.toString()); } @Test public void subtractSetToListTest() { - HashMap map1 = MapUtil.newHashMap(); - HashMap map2 = MapUtil.newHashMap(); + final HashMap map1 = MapUtil.newHashMap(); + final HashMap map2 = MapUtil.newHashMap(); map1.put("1", "v1"); map1.put("2", "v2"); map2.put("2", "v2"); - List r2 = CollUtil.subtractToList(map1.keySet(), map2.keySet()); + final List r2 = CollUtil.subtractToList(map1.keySet(), map2.keySet()); Assert.assertEquals("[1]", r2.toString()); } @Test public void toMapListAndToListMapTest() { - HashMap map1 = new HashMap<>(); + final HashMap map1 = new HashMap<>(); map1.put("a", "值1"); map1.put("b", "值1"); - HashMap map2 = new HashMap<>(); + final HashMap map2 = new HashMap<>(); map2.put("a", "值2"); map2.put("c", "值3"); // ---------------------------------------------------------------------------------------- - ArrayList> list = CollUtil.newArrayList(map1, map2); - Map> map = CollUtil.toListMap(list); + final ArrayList> list = CollUtil.newArrayList(map1, map2); + final Map> map = CollUtil.toListMap(list); Assert.assertEquals("值1", map.get("a").get(0)); Assert.assertEquals("值2", map.get("a").get(1)); // ---------------------------------------------------------------------------------------- - List> listMap = CollUtil.toMapList(map); + final List> listMap = CollUtil.toMapList(map); Assert.assertEquals("值1", listMap.get(0).get("a")); Assert.assertEquals("值2", listMap.get(1).get("a")); } @Test public void getFieldValuesTest() { - Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23); - Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); - ArrayList list = CollUtil.newArrayList(v1, v2); + final Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23); + final Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); + final ArrayList list = CollUtil.newArrayList(v1, v2); - List fieldValues = CollUtil.getFieldValues(list, "name"); + final List fieldValues = CollUtil.getFieldValues(list, "name"); Assert.assertEquals("张三", fieldValues.get(0)); Assert.assertEquals("李四", fieldValues.get(1)); } @@ -247,20 +247,20 @@ public class CollUtilTest { @Test public void splitTest() { final ArrayList list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); - List> split = CollUtil.split(list, 3); + final List> split = CollUtil.split(list, 3); Assert.assertEquals(3, split.size()); Assert.assertEquals(3, split.get(0).size()); } @Test public void foreachTest() { - HashMap map = MapUtil.newHashMap(); + final HashMap map = MapUtil.newHashMap(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); final String[] result = new String[1]; - String a = "a"; + final String a = "a"; CollUtil.forEach(map, (key, value, index) -> { if (a.equals(key)) { result[0] = value; @@ -271,18 +271,18 @@ public class CollUtilTest { @Test public void filterTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c"); + final ArrayList list = CollUtil.newArrayList("a", "b", "c"); - Collection filtered = CollUtil.edit(list, t -> t + 1); + final Collection filtered = CollUtil.edit(list, t -> t + 1); Assert.assertEquals(CollUtil.newArrayList("a1", "b1", "c1"), filtered); } @Test public void filterTest2() { - ArrayList list = CollUtil.newArrayList("a", "b", "c"); + final ArrayList list = CollUtil.newArrayList("a", "b", "c"); - ArrayList filtered = CollUtil.filter(list, t -> false == "a".equals(t)); + final ArrayList filtered = CollUtil.filter(list, t -> false == "a".equals(t)); // 原地过滤 Assert.assertSame(list, filtered); @@ -291,18 +291,18 @@ public class CollUtilTest { @Test public void filterSetTest() { - Set set = CollUtil.newLinkedHashSet("a", "b", "", " ", "c"); - Set filtered = CollUtil.filter(set, StrUtil::isNotBlank); + final Set set = CollUtil.newLinkedHashSet("a", "b", "", " ", "c"); + final Set filtered = CollUtil.filter(set, StrUtil::isNotBlank); Assert.assertEquals(CollUtil.newLinkedHashSet("a", "b", "c"), filtered); } @Test public void filterRemoveTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c"); + final ArrayList list = CollUtil.newArrayList("a", "b", "c"); - List removed = new ArrayList<>(); - ArrayList filtered = CollUtil.filter(list, t -> { + final List removed = new ArrayList<>(); + final ArrayList filtered = CollUtil.filter(list, t -> { if("a".equals(t)){ removed.add(t); return false; @@ -320,9 +320,9 @@ public class CollUtilTest { @Test public void removeNullTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); - ArrayList filtered = CollUtil.removeNull(list); + final ArrayList filtered = CollUtil.removeNull(list); // 原地过滤 Assert.assertSame(list, filtered); @@ -331,9 +331,9 @@ public class CollUtilTest { @Test public void removeEmptyTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); - ArrayList filtered = CollUtil.removeEmpty(list); + final ArrayList filtered = CollUtil.removeEmpty(list); // 原地过滤 Assert.assertSame(list, filtered); @@ -342,9 +342,9 @@ public class CollUtilTest { @Test public void removeBlankTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); - ArrayList filtered = CollUtil.removeBlank(list); + final ArrayList filtered = CollUtil.removeBlank(list); // 原地过滤 Assert.assertSame(list, filtered); @@ -353,11 +353,11 @@ public class CollUtilTest { @Test public void groupTest() { - List list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6"); - List> group = CollUtil.group(list, null); + final List list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6"); + final List> group = CollUtil.group(list, null); Assert.assertTrue(group.size() > 0); - List> group2 = CollUtil.group(list, t -> { + final List> group2 = CollUtil.group(list, t -> { // 按照奇数偶数分类 return Integer.parseInt(t) % 2; }); @@ -367,8 +367,8 @@ public class CollUtilTest { @Test public void groupByFieldTest() { - List list = CollUtil.newArrayList(new TestBean("张三", 12), new TestBean("李四", 13), new TestBean("王五", 12)); - List> groupByField = CollUtil.groupByField(list, "age"); + final List list = CollUtil.newArrayList(new TestBean("张三", 12), new TestBean("李四", 13), new TestBean("王五", 12)); + final List> groupByField = CollUtil.groupByField(list, "age"); Assert.assertEquals("张三", groupByField.get(0).get(0).getName()); Assert.assertEquals("王五", groupByField.get(0).get(1).getName()); @@ -377,7 +377,7 @@ public class CollUtilTest { @Test public void sortByPropertyTest() { - List list = CollUtil.newArrayList( + final List list = CollUtil.newArrayList( new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// @@ -391,7 +391,7 @@ public class CollUtilTest { @Test public void sortByPropertyTest2() { - List list = CollUtil.newArrayList( + final List list = CollUtil.newArrayList( new TestBean("张三", 0, DateUtil.parse("2018-05-01")), // new TestBean("李四", -12, DateUtil.parse("2018-03-01")), // new TestBean("王五", 23, DateUtil.parse("2018-04-01"))// @@ -405,7 +405,7 @@ public class CollUtilTest { @Test public void fieldValueMapTest() { - List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // + final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// ); @@ -418,7 +418,7 @@ public class CollUtilTest { @Test public void fieldValueAsMapTest() { - List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // + final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // new TestBean("王五", 14, DateUtil.parse("2018-04-01"))// ); @@ -448,7 +448,7 @@ public class CollUtilTest { private int age; private Date createTime; - public TestBean(String name, int age) { + public TestBean(final String name, final int age) { this.name = name; this.age = age; } @@ -456,8 +456,8 @@ public class CollUtilTest { @Test public void listTest() { - List list1 = CollUtil.list(false); - List list2 = CollUtil.list(true); + final List list1 = CollUtil.list(false); + final List list2 = CollUtil.list(true); Assert.assertTrue(list1 instanceof ArrayList); Assert.assertTrue(list2 instanceof LinkedList); @@ -465,28 +465,28 @@ public class CollUtilTest { @Test public void listTest2() { - List list1 = CollUtil.list(false, "a", "b", "c"); - List list2 = CollUtil.list(true, "a", "b", "c"); + final List list1 = CollUtil.list(false, "a", "b", "c"); + final List list2 = CollUtil.list(true, "a", "b", "c"); Assert.assertEquals("[a, b, c]", list1.toString()); Assert.assertEquals("[a, b, c]", list2.toString()); } @Test public void listTest3() { - HashSet set = new LinkedHashSet<>(); + final HashSet set = new LinkedHashSet<>(); set.add("a"); set.add("b"); set.add("c"); - List list1 = CollUtil.list(false, set); - List list2 = CollUtil.list(true, set); + final List list1 = CollUtil.list(false, set); + final List list2 = CollUtil.list(true, set); Assert.assertEquals("[a, b, c]", list1.toString()); Assert.assertEquals("[a, b, c]", list2.toString()); } @Test public void getTest() { - HashSet set = CollUtil.set(true, "A", "B", "C", "D"); + final HashSet set = CollUtil.set(true, "A", "B", "C", "D"); String str = CollUtil.get(set, 2); Assert.assertEquals("C", str); @@ -496,10 +496,10 @@ public class CollUtilTest { @Test public void addAllIfNotContainsTest() { - ArrayList list1 = new ArrayList<>(); + final ArrayList list1 = new ArrayList<>(); list1.add("1"); list1.add("2"); - ArrayList list2 = new ArrayList<>(); + final ArrayList list2 = new ArrayList<>(); list2.add("2"); list2.add("3"); CollUtil.addAllIfNotContains(list1, list2); @@ -687,45 +687,45 @@ public class CollUtilTest { @Test public void sortPageAllTest() { - List list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); - List sortPageAll = CollUtil.sortPageAll(1, 5, Comparator.reverseOrder(), list); + final List list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); + final List sortPageAll = CollUtil.sortPageAll(1, 5, Comparator.reverseOrder(), list); Assert.assertEquals(CollUtil.newArrayList(4, 3, 2, 1), sortPageAll); } @Test public void containsAnyTest() { - ArrayList list1 = CollUtil.newArrayList(1, 2, 3, 4, 5); - ArrayList list2 = CollUtil.newArrayList(5, 3, 1, 9, 11); + final ArrayList list1 = CollUtil.newArrayList(1, 2, 3, 4, 5); + final ArrayList list2 = CollUtil.newArrayList(5, 3, 1, 9, 11); Assert.assertTrue(CollUtil.containsAny(list1, list2)); } @Test public void containsAllTest() { - ArrayList list1 = CollUtil.newArrayList(1, 2, 3, 4, 5); - ArrayList list2 = CollUtil.newArrayList(5, 3, 1); + final ArrayList list1 = CollUtil.newArrayList(1, 2, 3, 4, 5); + final ArrayList list2 = CollUtil.newArrayList(5, 3, 1); Assert.assertTrue(CollUtil.containsAll(list1, list2)); - ArrayList list3 = CollUtil.newArrayList(1); - ArrayList list4 = CollUtil.newArrayList(); + final ArrayList list3 = CollUtil.newArrayList(1); + final ArrayList list4 = CollUtil.newArrayList(); Assert.assertTrue(CollUtil.containsAll(list3, list4)); } @Test public void getLastTest() { // 测试:空数组返回null而不是报错 - List test = CollUtil.newArrayList(); - String last = CollUtil.getLast(test); + final List test = CollUtil.newArrayList(); + final String last = CollUtil.getLast(test); Assert.assertNull(last); } @Test public void zipTest() { - Collection keys = CollUtil.newArrayList("a", "b", "c", "d"); - Collection values = CollUtil.newArrayList(1, 2, 3, 4); + final Collection keys = CollUtil.newArrayList("a", "b", "c", "d"); + final Collection values = CollUtil.newArrayList(1, 2, 3, 4); - Map map = CollUtil.zip(keys, values); + final Map map = CollUtil.zip(keys, values); Assert.assertEquals(4, Objects.requireNonNull(map).size()); @@ -737,7 +737,7 @@ public class CollUtilTest { @Test public void toMapTest() { - Collection keys = CollUtil.newArrayList("a", "b", "c", "d"); + final Collection keys = CollUtil.newArrayList("a", "b", "c", "d"); final Map map = CollUtil.toMap(keys, new HashMap<>(), (value) -> "key" + value); Assert.assertEquals("a", map.get("keya")); Assert.assertEquals("b", map.get("keyb")); @@ -764,8 +764,8 @@ public class CollUtilTest { @Test public void countMapTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); - Map countMap = CollUtil.countMap(list); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); + final Map countMap = CollUtil.countMap(list); Assert.assertEquals(Integer.valueOf(2), countMap.get("a")); Assert.assertEquals(Integer.valueOf(2), countMap.get("b")); @@ -775,7 +775,7 @@ public class CollUtilTest { @Test public void indexOfTest() { - ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); final int i = CollUtil.indexOf(list, (str) -> str.charAt(0) == 'c'); Assert.assertEquals(2, i); } @@ -783,14 +783,14 @@ public class CollUtilTest { @Test public void lastIndexOfTest() { // List有优化 - ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); + final ArrayList list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d"); final int i = CollUtil.lastIndexOf(list, (str) -> str.charAt(0) == 'c'); Assert.assertEquals(3, i); } @Test public void lastIndexOfSetTest() { - Set list = CollUtil.set(true, "a", "b", "c", "c", "a", "b", "d"); + final Set list = CollUtil.set(true, "a", "b", "c", "c", "a", "b", "d"); // 去重后c排第三 final int i = CollUtil.lastIndexOf(list, (str) -> str.charAt(0) == 'c'); Assert.assertEquals(2, i); @@ -798,7 +798,7 @@ public class CollUtilTest { @Test public void pageTest() { - List objects = CollUtil.newArrayList(); + final List objects = CollUtil.newArrayList(); for (int i = 0; i < 10; i++) { objects.add(Dict.create().set("name", "姓名:" + i)); } @@ -808,10 +808,10 @@ public class CollUtilTest { @Test public void subtractToListTest() { - List list1 = Arrays.asList(1L, 2L, 3L); - List list2 = Arrays.asList(2L, 3L); + final List list1 = Arrays.asList(1L, 2L, 3L); + final List list2 = Arrays.asList(2L, 3L); - List result = CollUtil.subtractToList(list1, list2); + final List result = CollUtil.subtractToList(list1, list2); Assert.assertEquals(1, result.size()); Assert.assertEquals(1L, result.get(0), 1); } @@ -826,7 +826,7 @@ public class CollUtilTest { @Test public void setValueByMapTest(){ // https://gitee.com/dromara/hutool/pulls/482 - List people = Arrays.asList( + final List people = Arrays.asList( new Person("aa", 12, "man", 1), new Person("bb", 13, "woman", 2), new Person("cc", 14, "man", 3), @@ -835,7 +835,7 @@ public class CollUtilTest { new Person("ff", 17, "man", 6) ); - Map genderMap = new HashMap<>(); + final Map genderMap = new HashMap<>(); genderMap.put(1, null); genderMap.put(2, "妇女"); genderMap.put(3, "少女"); @@ -847,7 +847,7 @@ public class CollUtilTest { CollUtil.setValueByMap(people, genderMap, Person::getId, Person::setGender); Assert.assertEquals(people.get(1).getGender(), "妇女"); - Map personMap = new HashMap<>(); + final Map personMap = new HashMap<>(); personMap.put(1, new Person("AA", 21, "男", 1)); personMap.put(2, new Person("BB", 7, "小孩", 2)); personMap.put(3, new Person("CC", 65, "老人", 3)); @@ -872,7 +872,7 @@ public class CollUtilTest { @Test public void distinctByFunctionTest(){ - List people = Arrays.asList( + final List people = Arrays.asList( new Person("aa", 12, "man", 1), new Person("bb", 13, "woman", 2), new Person("cc", 14, "man", 3), @@ -896,50 +896,50 @@ public class CollUtilTest { @Test public void unionNullTest() { - List list1 = new ArrayList<>(); - List list2 = null; - List list3 = null; - Collection union = CollUtil.union(list1, list2, list3); + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final Collection union = CollUtil.union(list1, list2, list3); Assert.assertNotNull(union); } @Test public void unionDistinctNullTest() { - List list1 = new ArrayList<>(); - List list2 = null; - List list3 = null; - Set set = CollUtil.unionDistinct(list1, list2, list3); + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final Set set = CollUtil.unionDistinct(list1, list2, list3); Assert.assertNotNull(set); } @Test public void unionAllNullTest() { - List list1 = new ArrayList<>(); - List list2 = null; - List list3 = null; - List list = CollUtil.unionAll(list1, list2, list3); + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); } @Test public void intersectionNullTest() { - List list1 = new ArrayList<>(); + final List list1 = new ArrayList<>(); list1.add("aa"); - List list2 = new ArrayList<>(); + final List list2 = new ArrayList<>(); list2.add("aa"); - List list3 = null; - Collection collection = CollUtil.intersection(list1, list2, list3); + final List list3 = null; + final Collection collection = CollUtil.intersection(list1, list2, list3); Assert.assertNotNull(collection); } @Test public void intersectionDistinctNullTest() { - List list1 = new ArrayList<>(); + final List list1 = new ArrayList<>(); list1.add("aa"); - List list2 = null; + final List list2 = null; // list2.add("aa"); - List list3 = null; - Collection collection = CollUtil.intersectionDistinct(list1, list2, list3); + final List list3 = null; + final Collection collection = CollUtil.intersectionDistinct(list1, list2, list3); Assert.assertNotNull(collection); }