commons-collections包中的經常使用的工具類

commons-collections包中的經常使用的工具類

 

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

 

 

 

 

1. CollectionUtils工具類用於操做集合,  isEmpty () 方法最有用   (commons-collections包中的類)

複製代碼
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");

        // 判斷是否有任何一個相同的元素
        System.out.println(CollectionUtils.containsAny(list, list1));

        // 求並集(自動去重)
        List<String> list3 = (List<String>) CollectionUtils.union(list, list1);
        System.out.println(list3);

        // 求交集(兩個集合中都有的元素)
        Collection intersection = CollectionUtils.intersection(list, list1);
        System.out.println("intersection->" + intersection);

        // 求差集(並集去掉交集,也就是list中有list1中沒有,list1中有list中沒有)
        Collection intersection1 = CollectionUtils.disjunction(list, list1);
        System.out.println("intersection1->" + intersection1);

        // 獲取一個同步的集合
        Collection synchronizedCollection = CollectionUtils.synchronizedCollection(list);

        // 驗證集合是否爲null或者集合的大小是否爲0,同理有isNouEmpty方法
        List list4 = null;
        List list5 = new ArrayList<>();
        System.out.println(CollectionUtils.isEmpty(list4));
        System.out.println(CollectionUtils.isEmpty(list5));
    }
}
複製代碼

結果:java

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

 

補充:此工具類還能夠向集合中加數組元素數組

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

結果:

[1, 2, 3]工具

 

2.   MapUtils工具類,isEmpty最有用(commons-collections包中的類)

  能夠用於map判斷null和size爲0,也能夠直接獲取map中的值爲指定類型,沒有的返回nullspa

複製代碼
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;

import ognl.MapElementsAccessor;

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);
    }
}
複製代碼

結果:code

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

 

 MapUtils.isEmpty根蹤源碼:字符串

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

 

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

 

補充:MapUtils也能夠獲取值做爲String,獲取不到取默認值:get

        //獲取字符串,若是獲取不到能夠返回一個默認值
        String string3 = MapUtils.getString(map3, "eee","沒有值");

 

 查看源碼:同步

複製代碼
    /**
     *  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;
    }
複製代碼
相關文章
相關標籤/搜索