CollectionUtils,ArrayUtils和StringUtils

    CollectionUtils,ArrayUtils和StringUtils分别是collection,array和字符串的工具类,里面定义了很多常用的工具函数。这里我们仅仅看一下collection工具类的一部分函数,更多了解请参考API文档。

1 CollectionUtils

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

import org.apache.commons.collections.CollectionUtils;

public class LearnCollection {
	public static void main(String[] args) {
		List<Integer> num1=new ArrayList<Integer>();
		num1.addAll(Arrays.asList(1,2,3,3));
		System.out.println("num1:"+num1);
		
		List<Integer> num2=new ArrayList<Integer>();
		num2.addAll(Arrays.asList(3,3,3,4,5));
		System.out.println("num2:"+num2);
		
		/**
		 * union  并集
		 * The cardinality of each element in the returned Collection will be 
		 * equal to the maximum of the cardinality of that element in the two 
		 * given Collections.
		 */
		Collection<Integer> res1=CollectionUtils.union(num1, num2);
		System.out.println("并集:"+res1);
		
		/**
		 * intersection  交集    
		 * The cardinality of each element in the returned Collection will be 
		 * equal to the minimum of the cardinality of that element in the two given 
		 * Collections.
		 * 
		 */
		Collection<Integer> res2=CollectionUtils.intersection(num1, num2);
		System.out.println("交集:"+res2);
		/**
		 * 是否存在交集  containsAny
		 */
		System.out.println("是否存在交集:"+CollectionUtils.containsAny(num1, num2));
		
		/**
		 * 交集的补集     disjunction
		 * equivalent to subtract(union(a,b),intersection(a,b)) 
		 */
		Collection<Integer> res3=CollectionUtils.disjunction(num1, num2);
		System.out.println("交集的补集:"+res3);
		/**
		 * 差集   subtract
		 * The cardinality of each element e in the returned Collection 
		 * will be the cardinality of e in a minus the cardinality of e in b, or zero
		 */
		Collection<Integer> res4=CollectionUtils.subtract(num2, num1);
		System.out.println("差集:"+res4);
		/**
		 * 判断是否是子集   集合A中每个元素的个数都大于另一个B中对应元素的个数,B是A的子集
		 */
		System.out.println("num1是不是num2的子集:"+CollectionUtils.isSubCollection(num1, num2));
		
		
		/**
		 * 还有很多其他函数,不一一列举
		 */
		/**
		 * 返回一个map,统计每个元素出现的次数
		 * Returns a Map mapping each unique element in the given Collection 
		 * to an Integer representing the number of occurrences of that element 
		 * in the Collection.
		 */
		Map<Integer, Integer> map=CollectionUtils.getCardinalityMap(num2);
		System.out.println("元素--个数:"+map);
		
	}

}

2 ArrayUtils

可以参考这篇文章: http://www.blogjava.net/windonly/archive/2008/02/26/182278.html

3 StringUtils

可以参考这篇文章,这里不再赘述: http://www.open-open.com/lib/view/open1326333409999.html

你可能感兴趣的:(StringUtils,工具类,ArrayUtils,CollectionUtils)