一、CollectionUtils
1.集合判断:
例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): true   CollectionUtils.isEmpty({a,b}): false 例2: 判断集合是否不为空: CollectionUtils.isNotEmpty(null): false CollectionUtils.isNotEmpty(new ArrayList()): false CollectionUtils.isNotEmpty({a,b}): true
2.并集
union()方法
@Test public void testUnion(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取并集 System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB))); //[A, B, C, D, E, F, G, H, K] }
3.交集
intersection()方法
@Test public void testIntersection(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取交集 System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB))); //[B, D, F] }
4.交集的补集(析取)
@Test public void testDisjunction(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取交集 的补集 System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB))); //[A, C, E, G, H, K] }
5.差集(扣除)
@Test public void testSubtract(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //arrayA扣除arrayB System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB))); //[A, C, E] }
6.集合是否相等(相当于==号)
- 集合是否为空
@Test public void testIsEmpty(){ class Person{} class Girl extends Person{} List<Integer> first = new ArrayList<>(); List<Integer> second = null; List<Person> boy = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy.add(new Girl()); //判断集合是否为空 System.out.println(CollectionUtils.isEmpty(first)); //true System.out.println(CollectionUtils.isEmpty(second)); //true System.out.println(CollectionUtils.isEmpty(boy)); //false //判断集合是否不为空 System.out.println(CollectionUtils.isNotEmpty(first)); //false System.out.println(CollectionUtils.isNotEmpty(second)); //false System.out.println(CollectionUtils.isNotEmpty(boy)); //true }
- 集合是否相等(相当于==号)
@Test public void testIsEqual(){ class Person{} class Girl extends Person{ } List<Integer> first = new ArrayList<>(); List<Integer> second = new ArrayList<>(); first.add(1); first.add(2); second.add(2); second.add(1); Girl goldGirl = new Girl(); List<Person> boy1 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy1.add(new Girl()); List<Person> boy2 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy2.add(new Girl()); //比较两集合值 System.out.println(CollectionUtils.isEqualCollection(first,second)); //true System.out.println(CollectionUtils.isEqualCollection(first,boy1)); //false System.out.println(CollectionUtils.isEqualCollection(boy1,boy2)); //false List<Person> boy3 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy3.add(goldGirl); List<Person> boy4 = new ArrayList<>(); boy4.add(goldGirl); System.out.println(CollectionUtils.isEqualCollection(boy3,boy4)); //true }
7.不可修改的集合
我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。有时候需要对它进行保护,避免返回结果被人修改。
@Test public void testUnmodifiableCollection(){ Collection<String> c = new ArrayList<>(); Collection<String> s = CollectionUtils.unmodifiableCollection(c); c.add("boy"); c.add("love"); c.add("girl"); //! s.add("have a error"); System.out.println(s); }
Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误
java.lang.UnsupportedOperationException at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)
二、MapUtils
isEmpty()可以用于map判断null和size为0, getString()也可以直接获取map中的值为指定类型,没有的返回null
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)); //true System.out.println(MapUtils.isEmpty(map2)); //true System.out.println(MapUtils.isEmpty(map3)); //false 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);
输出结果为:
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.isEmpry()代码为 public boolean isEmpty() { return size == 0; }
- 补充:MapUtils也可以获取值作为String,获取不到取默认值:
//获取字符串,如果获取不到可以返回一个默认值 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; }