commons-collections package commonly used tools

commons-collections package commonly used tools

 

       <dependency>
          <groupId>commons-collections</groupId>
          <artifactId>commons-collections</artifactId>
       </dependency>

 

 

 

 

1. CollectionUtils set of tools for operation, isEmpty () method is most useful (commons-collections classes in a package)

Copy the code
package cn.xm.exam.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

public class test {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("str1");
        list.add("str2");

        List<String> list1 = new ArrayList<String>();
        list1.add("str1");
        list1.add("str21");

        // determine if any of the same elements
        System.out.println(CollectionUtils.containsAny(list, list1));

        // find and set (automatic de-emphasis)
        List<String> list3 = (List<String>) CollectionUtils.union(list, list1);
        System.out.println(list3);

        // find intersection (there are two sets of elements)
        Collection intersection = CollectionUtils.intersection(list, list1);
        System.out.println("intersection->" + intersection);

        // differencing set (set and remove the intersection, i.e. list1 there is no list, list1 there is no list)
        Collection intersection1 = CollectionUtils.disjunction(list, list1);
        System.out.println("intersection1->" + intersection1);

        // Get a set of synchronization
        Collection synchronizedCollection = CollectionUtils.synchronizedCollection(list);

        // null validation set whether or size of the set is zero, there Similarly method isNouEmpty
        List list4 = null;
        List list5 = new ArrayList<>();
        System.out.println(CollectionUtils.isEmpty(list4));
        System.out.println(CollectionUtils.isEmpty(list5));
    }
}
Copy the code

result:

true
[str2, str21, str1]
intersection->[str1]
intersection1->[str2, str21]
true
true

 

NOTE: This utility class can also be added to the collection of array elements

        List<String> list = new ArrayList<>();
        String s[] = { "1", "2" };
        CollectionUtils.addAll(list, s);
        list.add("3");
        System.out.println(list);

result:

[1, 2, 3]

 

2. MapUtils tools, isEmpty most useful (commons-collections classes in a package)

  Analyzing null and can be used to map size is 0, the value of the specified type can also be obtained directly in the map, there is no return null

Copy the code
package cn.xm.exam.test;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.NumberUtils;

ognl.MapElementsAccessor import;

public class test {
    public static void main(String[] args) {
        Map map = null;
        Map map2 = new HashMap();
        Map map3 = new HashMap<>();
        map3.put("xxx", "xxx");
        // 检验为empty可以验证null和size为0的情况
        System.out.println(MapUtils.isEmpty(map));
        System.out.println(MapUtils.isEmpty(map2));
        System.out.println(MapUtils.isEmpty(map3));

        String string = MapUtils.getString(map3, "eee");
        String string2 = MapUtils.getString(map3, "xxx");
        Integer integer = MapUtils.getInteger(map3, "xxx");
        System.out.println("string->" + string);
        System.out.println("string2->" + string2);
        System.out.println("integer->" + integer);
        System.out.println(integer == null);
    }
}
Copy the code

结果:

true
true
false
INFO: Exception: java.text.ParseException: Unparseable number: "xxx"
string->null
string2->xxx
integer->null
true

 

 MapUtils.isEmpty根踪源码:

    public static boolean isEmpty(Map map) {
        return (map == null || map.isEmpty());
    }

 

map.isEmpty()代码查看hashmap:
    public boolean isEmpty() {
        return size == 0;
    }

 

补充:MapUtils也可以获取值作为String,获取不到取默认值:

        //获取字符串,如果获取不到可以返回一个默认值
        String string3 = MapUtils.getString(map3, "eee","没有值");

 

 查看源码:

Copy the code
    /**
     *  Looks up the given key in the given map, converting the result into
     *  a string, using the default value if the the conversion fails.
     *
     *  @param map  the map whose value to look up
     *  @param key  the key of the value to look up in that map
     *  @param defaultValue  what to return if the value is null or if the
     *     conversion fails
     *  @return  the value in the map as a string, or defaultValue if the 
     *    original value is null, the map is null or the string conversion
     *    fails
     */
    public static String getString( Map map, Object key, String defaultValue ) {
        String answer = getString( map, key );
        if ( answer == null ) {
            answer = defaultValue;
        }
        return answer;
    }
Copy the code

 

 

Guess you like

Origin www.cnblogs.com/dw3306/p/11033204.html