boolean hasUniqueObject(Collection collection)
From the source code comments, it is used to judge List/Set Whether each element in the List/Set is unique, that is, there are no duplicate elements in the List/Set. But here I want to tell you not to use this method, because this method has bugs, why? The following is the source code in Spring-core-5.2.13.RELEASE.jar. Look at line 12. Careful people will find that != is used to compare whether two objects are equal. Remember the difference between "==" and "equals"? The "==" operator is specially used to compare whether the values of two variables are equal, and the equals() method is used to compare whether the contents of two independent objects are the same. So if the elements in the collection are numerical values, you can use "==" for comparison. If they are ordinary reference objects, you will not get the correct result.
public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
boolean containsInstance(Collection collection, Object element)
From the source code comments, it is used to determine whether the collection contains an object. This method is not recommended because it has the same problem as the previous method. Looking at the 4th line of the source code, "==" is still used.
public static boolean containsInstance(@Nullable Collection<?> collection, Object element) { if (collection != null) { for (Object candidate : collection) { if (candidate == element) { return true; } } } return false; }
boolean isEmpty(Collection collection)
This method has been verified and can be used with confidence. It is used to determine whether the List/Set is empty;
@Test public void test1(){ Collection<String> list=new ArrayList<>(); boolean empty = CollectionUtils.isEmpty(list); Assert.isTrue(empty, "集合list不为空"); System.out.println("集合list增加一元素"); list.add("happy"); boolean empty2 = CollectionUtils.isEmpty(list); Assert.isTrue(empty2, "集合list不为空"); }
boolean isEmpty(Map map )
is used to determine whether the Map is empty.
@Test public void test2(){ Map<String,String> map = new HashMap<>(); boolean empty = CollectionUtils.isEmpty(map); Assert.isTrue(empty, "map不为空"); System.out.println("map中增加元素"); map.put("name", "jack"); boolean empty2 = CollectionUtils.isEmpty(map); Assert.isTrue(empty2, "map不为空"); }
boolean containsAny(Collection source, Collection candidates)
From the comments on the source code, it is used to determine whether the collection source contains any element of another collection candidates, that is, the collection candidates Whether the elements in are completely contained in the set souce.
From the source code, the comparison between elements in this method uses the "equals" method, and the equals method of the object in the collection is called. Therefore, the premise of using this method to get the correct result is to compare The object needs to override the hashCode() and eauals() methods.
@Test public void test4(){ Employee lisi = new Employee("lisi"); Employee zhangsan = new Employee("zhangsan"); Employee wangwu = new Employee("wangwu"); List<Employee > list=new ArrayList<>(); list.add(zhangsan); list.add(lisi); List<Employee> list2=new ArrayList<>(); list2.add(wangwu); //这里可以用是因为比较的时候调用的是equals方法 boolean b = CollectionUtils.containsAny(list, list2); Assert.isTrue(b, "list1没有包含有list2中任意一个元素"); }
void mergeArrayIntoCollection(Object array, Collection collection)
Add all the elements in the array array to the List/Set.
@Test public void test6(){ List<Employee > list=new ArrayList<>(); Employee lisi = new Employee("lisi"); list.add(lisi); Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; CollectionUtils.mergeArrayIntoCollection(employees, list); Assert.isTrue(list.size()==2, "把数据中的元素合并到list失败了"); }
void mergePropertiesIntoMap(Properties props, Map map)
Add all key-value pairs in Properties to Map.
@Test public void test7(){ Properties properties = new Properties(); properties.setProperty("name", "zhangsan"); Map<String,String > map = new HashMap<>(); CollectionUtils.mergePropertiesIntoMap(properties, map); Assert.isTrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了"); } @Test public void test7(){ Properties properties = new Properties(); properties.setProperty("name", "zhangsan"); Map<String,String > map = new HashMap<>(); CollectionUtils.mergePropertiesIntoMap(properties, map); Assert.isTrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了"); }
T lastElement(List list)
Returns the last element in List.
@Test public void test9(){ Employee lisi = new Employee("lisi"); Employee zhangsan = new Employee("zhangsan"); List<Employee > list=new ArrayList<>(); list.add(zhangsan); list.add(lisi); Employee employee = CollectionUtils.firstElement(list); Assert.isTrue(employee.equals(zhangsan), "获取集合第一个元素失败了"); }
T firstElement(List list)
Returns the first element in the collection.
@Test public void test10(){ Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; List list = CollectionUtils.arrayToList(employees); Assert.isTrue(list.size()==1, "把数据转换成集合失败了"); }
List arrayToList(Object source)
Convert an array into a collection.
@Test public void test10(){ Employee zhangsan = new Employee("zhangsan"); Employee[] employees={zhangsan}; List list = CollectionUtils.arrayToList(employees); Assert.isTrue(list.size()==1, "把数据转换成集合失败了"); }
The above is the detailed content of How to use Springboot’s built-in tool class CollectionUtils. For more information, please follow other related articles on the PHP Chinese website!