当前位置 : 主页 > 编程语言 > java >

Collections工具集

来源:互联网 收集:自由互联 发布时间:2021-06-28
Collections工具集 /** * Copyright (c) 2005-2012 springside.org.cn */package *;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org
Collections工具集
/**
 * Copyright (c) 2005-2012 springside.org.cn
 */
package *;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Collections工具集.
 * 在JDK的Collections和Guava的Collections2后, 命名为Collections3.
 * @author calvin
 * @version 2013-01-15
 */
@SuppressWarnings("rawtypes")
public class Collections3 {

	/**
	 * 提取集合中的对象的两个属性(通过Getter函数), 组合成Map.
	 * 
	 * @param collection 来源集合.
	 * @param keyPropertyName 要提取为Map中的Key值的属性名.
	 * @param valuePropertyName 要提取为Map中的Value值的属性名.
	 */
	@SuppressWarnings("unchecked")
	public static Map extractToMap(final Collection collection, final String keyPropertyName,
			final String valuePropertyName) {
		Map map = new HashMap(collection.size());

		try {
			for (Object obj : collection) {
				map.put(PropertyUtils.getProperty(obj, keyPropertyName),
						PropertyUtils.getProperty(obj, valuePropertyName));
			}
		} catch (Exception e) {
			throw Reflections.convertReflectionExceptionToUnchecked(e);
		}

		return map;
	}

	/**
	 * 提取集合中的对象的一个属性(通过Getter函数), 组合成List.
	 * 
	 * @param collection 来源集合.
	 * @param propertyName 要提取的属性名.
	 */
	@SuppressWarnings("unchecked")
	public static List extractToList(final Collection collection, final String propertyName) {
		List list = new ArrayList(collection.size());

		try {
			for (Object obj : collection) {
				list.add(PropertyUtils.getProperty(obj, propertyName));
			}
		} catch (Exception e) {
			throw Reflections.convertReflectionExceptionToUnchecked(e);
		}

		return list;
	}

	/**
	 * 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串.
	 * 
	 * @param collection 来源集合.
	 * @param propertyName 要提取的属性名.
	 * @param separator 分隔符.
	 */
	public static String extractToString(final Collection collection, final String propertyName, final String separator) {
		List list = extractToList(collection, propertyName);
		return StringUtils.join(list, separator);
	}

	/**
	 * 转换Collection所有元素(通过toString())为String, 中间以 separator分隔。
	 */
	public static String convertToString(final Collection collection, final String separator) {
		return StringUtils.join(collection, separator);
	}

	/**
	 * 转换Collection所有元素(通过toString())为String, 每个元素的前面加入prefix,后面加入postfix,如
 
  mymessage
 。
	 */
	public static String convertToString(final Collection collection, final String prefix, final String postfix) {
		StringBuilder builder = new StringBuilder();
		for (Object o : collection) {
			builder.append(prefix).append(o).append(postfix);
		}
		return builder.toString();
	}

	/**
	 * 判断是否为空.
	 */
	public static boolean isEmpty(Collection collection) {
		return (collection == null || collection.isEmpty());
	}

	/**
	 * 取得Collection的第一个元素,如果collection为空返回null.
	 */
	public static 
 
   T getFirst(Collection
  
    collection) { if (isEmpty(collection)) { return null; } return collection.iterator().next(); } /** * 获取Collection的最后一个元素 ,如果collection为空返回null. */ public static 
   
     T getLast(Collection
    
      collection) { if (isEmpty(collection)) { return null; } //当类型为List时,直接取得最后一个元素 。 if (collection instanceof List) { List
     
       list = (List
      
       ) collection; return list.get(list.size() - 1); } //其他类型通过iterator滚动到最后一个元素. Iterator
       
         iterator = collection.iterator(); while (true) { T current = iterator.next(); if (!iterator.hasNext()) { return current; } } } /** * 返回a+b的新List. */ public static 
        
          List
         
           union(final Collection
          
            a, final Collection
           
             b) { List
            
              result = new ArrayList
             
              (a); result.addAll(b); return result; } /** * 返回a-b的新List. */ public static 
              
                List
               
                 subtract(final Collection
                
                  a, final Collection
                 
                   b) { List
                  
                    list = new ArrayList
                   
                    (a); for (T element : b) { list.remove(element); } return list; } /** * 返回a与b的交集的新List. */ public static 
                    
                      List
                     
                       intersection(Collection
                      
                        a, Collection
                       
                         b) { List
                        
                          list = new ArrayList
                         
                          (); for (T element : a) { if (b.contains(element)) { list.add(element); } } return list; } }
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
上一篇:snowflake java实现
下一篇:卡片布局
网友评论