CollectUtils封装类——CollectUtils

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.commons.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.UnhandledException;

public final class CollectUtils {

	public static <E> List<E> newArrayList() {
		return new ArrayList<E>();
	}

	public static <E> List<E> newArrayList(int initialCapacity) {
		return new ArrayList<E>(initialCapacity);
	}

	public static <E> List<E> newArrayList(Collection<? extends E> c) {
		return new ArrayList<E>(c);
	}

	public static <E> List<E> newArrayList(E... values) {
		List<E> list = new ArrayList<E>(values.length);
		for (E e : values) {
			list.add(e);
		}
		return list;
	}

	/**
	 * 将一个集合按照固定大小查分成若干个集合。
	 * 
	 * @param list
	 * @param count
	 * @return
	 */
	public static <T> List<List<T>> split(final List<T> list, final int count) {
		List<List<T>> subIdLists = CollectUtils.newArrayList();
		if (list.size() < count) {
			subIdLists.add(list);
		} else {
			int i = 0;
			while (i < list.size()) {
				int end = i + count;
				if (end > list.size()) {
					end = list.size();
				}
				subIdLists.add(list.subList(i, end));
				i += count;
			}
		}
		return subIdLists;
	}

	public static <K, V> Map<K, V> newHashMap() {
		return new HashMap<K, V>();
	}

	public static <K, V> Map<K, V> newConcurrentHashMap() {
		return new ConcurrentHashMap<K, V>();
	}

	public static <E> Queue<E> newConcurrentLinkedQueue() {
		return new ConcurrentLinkedQueue<E>();
	}

	public static <K, V> Map<K, V> newHashMap(Map<? extends K, ? extends V> m) {
		return new HashMap<K, V>(m);
	}

	public static <K, V> Map<K, V> newLinkedHashMap(Map<? extends K, ? extends V> m) {
		return new LinkedHashMap<K, V>(m);
	}

	public static <K, V> Map<K, V> newLinkedHashMap(int size) {
		return new LinkedHashMap<K, V>(size);
	}

	public static <E> Set<E> newHashSet() {
		return new HashSet<E>();
	}

	public static <E> Set<E> newHashSet(E... values) {
		Set<E> set = new HashSet<E>(values.length);
		for (E e : values) {
			set.add(e);
		}
		return set;
	}

	public static <E> Set<E> newHashSet(Collection<? extends E> c) {
		return new HashSet<E>(c);
	}

	public static Map<?, ?> convertToMap(Collection<?> coll, String keyProperty) {
		Map<Object, Object> map = newHashMap();
		for (Object obj : coll) {
			Object key = null;
			try {
				key = PropertyUtils.getProperty(obj, keyProperty);
			} catch (Exception e) {
				throw new UnhandledException(e);
			}
			map.put(key, obj);
		}
		return map;
	}

	public static Map<?, ?> convertToMap(Collection<?> coll, String keyProperty, String valueProperty) {
		Map<Object, Object> map = newHashMap();
		for (Object obj : coll) {
			Object key = null;
			Object value = null;
			try {
				key = PropertyUtils.getProperty(obj, keyProperty);
				value = PropertyUtils.getProperty(obj, valueProperty);
			} catch (Exception e) {
				throw new UnhandledException(e);
			}
			map.put(key, value);
		}
		return map;
	}

	public static Map<String, String> toMap(String[]... wordMappings) {
		Map<String, String> mappings = new HashMap<String, String>();
		for (int i = 0; i < wordMappings.length; i++) {
			String singular = wordMappings[i][0];
			String plural = wordMappings[i][1];
			mappings.put(singular, plural);
		}
		return mappings;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值