import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 容器转换类,主要用于实现各种容器相互转换
*
*/
public class CollectionUtils {
/**
* UsefulTool 构造子注解。
*/
public CollectionUtils() {
super();
}
/**
* 将Hashtable中的键值转换为ArrayList
*
* @param ht
* @return
*/
@SuppressWarnings("unchecked")
public static ArrayList hashtableKeysToArrayList(java.util.Hashtable ht) {
java.util.Enumeration enum1 = ht.keys();
ArrayList alHtValue = new ArrayList();
Object obj;
while (enum1.hasMoreElements()) {
obj = enum1.nextElement();
alHtValue.add(obj);
}
return alHtValue;
}
/**
* 将Hashtable中的键值转换为字符串数组
*
* @param ht
* @return
*/
@SuppressWarnings("unchecked")
public static String[] hashtableKeysToStrings(java.util.Hashtable ht) {
java.util.Enumeration enum1 = ht.keys();
ArrayList alHtValue = new ArrayList();
String s;
while (enum1.hasMoreElements()) {
s = enum1.nextElement().toString();
alHtValue.add(s);
}
String[] sKeys = (String[]) alHtValue.toArray(new String[0]);
return sKeys;
}
/**
* 将hash中的值转化为ArrayList
*
* @param ht
* @return
*/
@SuppressWarnings("unchecked")
public static ArrayList hashtableValuesToArrayList(java.util.Hashtable ht) {
ArrayList alHtValue = new ArrayList();
java.util.Iterator itValues = ht.values().iterator();
while (itValues.hasNext()) {
Object obj = itValues.next();
alHtValue.add(obj);
}
return alHtValue;
}
/**
* 根据集合中真实的类型,转换为真实类型的数组
*
* @author yangjl
*
* @param <T>
* @param result
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T[] convertToRealTypeArray(Collection<T> result) {
if (result == null || result.size() == 0)
return null;
Class clazz = result.iterator().next().getClass();
return result.toArray((T[]) Array.newInstance(clazz, result.size()));
}
/**
* 将传入的对象转换成数组。
*
* @param result
* 结果集合。
* @param dists
* 目标数组。
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T[] convertToArray(Collection result, T[] dists) {
if (result == null)
return null;
return (T[]) result.toArray(dists);
}
/**
* 将传入的数组转换成Collection对象。
*
* @param result
* @return
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> convertToList(T[] result) {
if (result == null)
return new ArrayList<T>();
return new ArrayList(Arrays.asList(result));
}
/**
*
* @param result
* @return
*/
@SuppressWarnings("unchecked")
public static Object[] convertToArray(Collection result) {
return convertToArray(result, new Object[0]);
}
/**
* 将集合对象转化为数组,
*
* @param <T>
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> c) {
return convertToRealTypeArray(c);
}
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll
* the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
/**
*
* 判断Map类对象是否为空
* <p>
* <b>examples:</b>
* <p>
* 使用示例
* <p>
* <b>参数说明</b>
* @param map
* @return
* <p>
* @author taorz1
* @time 2010-10-28 上午09:59:19
*/
@Deprecated
//TODO MapUtils已经提供此项功能了
public static boolean isEmpty(Map map){
return (map == null || map.isEmpty());
}
/**
* Null-safe check if the specified collection is not empty.
* <p>
* Null returns false.
*
* @param coll
* the collection to check, may be null
* @return true if non-null and non-empty
* @since Commons Collections 3.2
*/
public static boolean isNotEmpty(Collection coll) {
return !isEmpty(coll);
}
/**
* 求两个容器对象中的并集。<br>
*
* <pre>
* 注意:容器对象中存在多个一样的对象。返回的值中只能存在一個。
* <br>
* 如果容器對象為list,那么可能会要求交集或并集存在多个相同的对象,
* <br>
* 此种情况请参考Apache Commons CollectionUtils中的union和insertsection方法。
* </pre>
*
* @param <T>
* @param c1
* @param c2
* @return 两个集合的并集。
* @author yanjq
*/
public static <T> Set<T> union(Collection<T> c1, Collection<T> c2) {
if (isEmpty(c1) && isEmpty(c2)) {
Set<T> set = Collections.emptySet();
return set;
}
if (isEmpty(c1))
return new HashSet<T>(c2);
if (isEmpty(c2))
return new HashSet<T>(c1);
Set<T> set = new HashSet<T>();
set.addAll(c1);
set.addAll(c2);
return set;
}
/**
* 求两个容器对象中的交集。<br>
*
* <pre>
* 注意:容器对象中存在多个一样的对象。返回的值中只能存在一個。
* <br>
* 如果容器對象為list,那么可能会要求交集或并集存在多个相同的对象,
* <br>
* 此种情况请参考Apache Commons CollectionUtils中的union和insertsection方法。
* </pre>
*
*
* @param <T>
* @param c1
* @param c2
* @return
* @author yanjq
*/
public static <T> Set<T> intersection(Collection<T> c1, Collection<T> c2) {
if (isEmpty(c1) || isEmpty(c2)) {
Set<T> set = Collections.emptySet();
return set;
}
Set<T> allSet = union(c1, c2);
Set<T> set = new HashSet<T>();
for (T t : allSet) {
if (c1.contains(t) && c2.contains(t))
set.add(t);
}
return set;
}
/**
* 添加多个collection的元素,新增加的元素中過濾掉null值
*
* @param <E>
* @param c1
* @param collections
* @author yanjq
*/
public static <E> void addAll(Collection<E> c1, Collection<? extends E>... collections) {
if (ArrayUtils.isEmpty(collections))
return;
for (Collection<? extends E> coll : collections) {
if (isNotEmpty(coll))
c1.addAll(coll);
}
}
/**
* 在collection中添加数组元素。过滤掉数组中的null值
*
* @param <E>
* @param <T>
* @param c
* @param ts
* @author yanjq
*/
public static <E, T extends E> void addAll(Collection<E> c, T... ts) {
if (ArrayUtils.isEmpty(ts))
return;
for (T t : ts) {
if (t != null)
c.add(t);
}
}
/**
* 求两个容器中的差集(a-b)
*
* @param a
* the collection to subtract from, must not be null
* @param b
* the collection to subtract
* @return a new collection with the results
* @see Collection#removeAll
* @author yanjq
*/
public static <E> Set<E> subtract(Collection<E> a, Collection<E> b) {
if (isEmpty(b))
return new HashSet<E>(a);
Set<E> set = new HashSet<E>(a);
set.removeAll(b);
return set;
}
/**
* 返回容器中的第一个元素
*
* @param <T>
* @param c
* @return
* @author yanjq
*/
public static <T> T getFirstElem(Collection<T> c) {
return isEmpty(c) ? null : c.iterator().next();
}
/**
* Returns <tt>true</tt> iff the given {@link Collection}s contain exactly
* the same elements with exactly the same cardinalities.
* <p>
* That is, iff the cardinality of <i>e</i> in <i>a</i> is equal to the
* cardinality of <i>e</i> in <i>b</i>, for each element <i>e</i> in
* <i>a</i> or <i>b</i>.
*
* @param a
* the first collection, must not be null
* @param b
* the second collection, must not be null
* @return <code>true</code> iff the collections contain the same elements
* with the same cardinalities.
*/
public static boolean isEqualCollection(final Collection a, final Collection b) {
if (a == null)
return b == null ? true : false;
if (b == null)
return false;
if (a.size() != b.size())
return false;
Map mapa = getCardinalityMap(a);
Map mapb = getCardinalityMap(b);
if (mapa.size() != mapb.size())
return false;
Iterator it = mapa.keySet().iterator();
while (it.hasNext()) {
Object obj = it.next();
if (getFreq(obj, mapa) != getFreq(obj, mapb)) {
return false;
}
}
return true;
}
/**
* Returns a {@link Map} mapping each unique element in the given
* {@link Collection} to an {@link Integer} representing the number of
* occurrences of that element in the {@link Collection}.
* <p>
* Only those elements present in the collection will appear as keys in the
* map.
*
* @param coll
* the collection to get the cardinality map for, must not be
* null
* @return the populated cardinality map
*/
public static <T> Map<T, Integer> getCardinalityMap(final Collection<T> coll) {
Map<T, Integer> count = new HashMap<T, Integer>();
for (Iterator<T> it = coll.iterator(); it.hasNext();) {
T obj = it.next();
Integer c = count.get(obj);
if (c == null) {
count.put(obj, Integer.valueOf(1));
} else {
count.put(obj, Integer.valueOf((c.intValue() + 1)));
}
}
return count;
}
private static final int getFreq(final Object key, final Map freqMap) {
Integer count = (Integer) freqMap.get(key);
if (count != null) {
return count.intValue();
}
return 0;
}
/**
* 判断两个集合是否存在交集
*
* @param <T>
* @param c1
* @param c2
* @return
* @author yanjq
* @date 2010-4-14
*/
public static final <T> boolean isHasInterSection(Collection<T> c1, Collection<T> c2) {
Set<T> set2 = new HashSet<T>(c2);
for (T t : c1) {
if (set2.contains(t))
return true;
}
return false;
}
}
CollectionUtils:容器转换类,可实现各种容器相互转换
最新推荐文章于 2024-08-03 22:39:33 发布