首页

通过集合工具类CollectionUtils实现合并数组、属性至集合或Map集、指定类型查找等相关操作

标签:集合工具类,CollectionUtils,合并数组属性,集合类型查找     发布时间:2018-06-17   

一、前言

这边通过定义CollectionUtils集合工具类,实现对数组转序列arrayToList、合并数组为集合mergeArrayIntoCollection、合并属性为Map的方法mergePropertiesIntoMap、集合类型查询findValueOfType等操作。

二、代码示例

import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Properties;@b@@b@@b@public abstract class CollectionUtils {@b@@b@	/**@b@	 * Return <code>true</code> if the supplied Collection is <code>null</code>@b@	 * or empty. Otherwise, return <code>false</code>.@b@	 * @param collection the Collection to check@b@	 * @return whether the given Collection is empty@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static boolean isEmpty(Collection collection) {@b@		return (collection == null || collection.isEmpty());@b@	}@b@@b@	/**@b@	 * Return <code>true</code> if the supplied Map is <code>null</code>@b@	 * or empty. Otherwise, return <code>false</code>.@b@	 * @param map the Map to check@b@	 * @return whether the given Map is empty@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static boolean isEmpty(Map map) {@b@		return (map == null || map.isEmpty());@b@	}@b@@b@	/**@b@	 * Convert the supplied array into a List. A primitive array gets@b@	 * converted into a List of the appropriate wrapper type.@b@	 * <p>A <code>null</code> source value will be converted to an@b@	 * empty List.@b@	 * @param source the (potentially primitive) array@b@	 * @return the converted List result@b@	 * @see ObjectUtils#toObjectArray(Object)@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static List arrayToList(Object source) {@b@		return Arrays.asList(ObjectUtils.toObjectArray(source));@b@	}@b@@b@	/**@b@	 * Merge the given array into the given Collection.@b@	 * @param array the array to merge (may be <code>null</code>)@b@	 * @param collection the target Collection to merge the array into@b@	 */@b@	@SuppressWarnings({"rawtypes","unchecked"})@b@	public static void mergeArrayIntoCollection(Object array, Collection collection) {@b@		if (collection == null) {@b@			throw new IllegalArgumentException("Collection must not be null");@b@		}@b@		Object[] arr = ObjectUtils.toObjectArray(array);@b@		for (Object elem : arr) {@b@			collection.add(elem);@b@		}@b@	}@b@@b@	/**@b@	 * Merge the given Properties instance into the given Map,@b@	 * copying all properties (key-value pairs) over.@b@	 * <p>Uses <code>Properties.propertyNames()</code> to even catch@b@	 * default properties linked into the original Properties instance.@b@	 * @param props the Properties instance to merge (may be <code>null</code>)@b@	 * @param map the target Map to merge the properties into@b@	 */@b@	@SuppressWarnings({"unchecked","rawtypes"})@b@	public static void mergePropertiesIntoMap(Properties props, Map map) {@b@		if (map == null) {@b@			throw new IllegalArgumentException("Map must not be null");@b@		}@b@		if (props != null) {@b@			for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {@b@				String key = (String) en.nextElement();@b@				Object value = props.getProperty(key);@b@				if (value == null) {@b@					// Potentially a non-String value...@b@					value = props.get(key);@b@				}@b@				map.put(key, value);@b@			}@b@		}@b@	}@b@@b@@b@	/**@b@	 * Check whether the given Iterator contains the given element.@b@	 * @param iterator the Iterator to check@b@	 * @param element the element to look for@b@	 * @return <code>true</code> if found, <code>false</code> else@b@	 */@b@	public static boolean contains(@SuppressWarnings("rawtypes") Iterator iterator, Object element) {@b@		if (iterator != null) {@b@			while (iterator.hasNext()) {@b@				Object candidate = iterator.next();@b@				if (ObjectUtils.nullSafeEquals(candidate, element)) {@b@					return true;@b@				}@b@			}@b@		}@b@		return false;@b@	}@b@@b@	/**@b@	 * Check whether the given Enumeration contains the given element.@b@	 * @param enumeration the Enumeration to check@b@	 * @param element the element to look for@b@	 * @return <code>true</code> if found, <code>false</code> else@b@	 */@b@	public static boolean contains(@SuppressWarnings("rawtypes") Enumeration enumeration, Object element) {@b@		if (enumeration != null) {@b@			while (enumeration.hasMoreElements()) {@b@				Object candidate = enumeration.nextElement();@b@				if (ObjectUtils.nullSafeEquals(candidate, element)) {@b@					return true;@b@				}@b@			}@b@		}@b@		return false;@b@	}@b@@b@	/**@b@	 * Check whether the given Collection contains the given element instance.@b@	 * <p>Enforces the given instance to be present, rather than returning@b@	 * <code>true</code> for an equal element as well.@b@	 * @param collection the Collection to check@b@	 * @param element the element to look for@b@	 * @return <code>true</code> if found, <code>false</code> else@b@	 */@b@	public static boolean containsInstance(@SuppressWarnings("rawtypes") Collection collection, Object element) {@b@		if (collection != null) {@b@			for (Object candidate : collection) {@b@				if (candidate == element) {@b@					return true;@b@				}@b@			}@b@		}@b@		return false;@b@	}@b@@b@	/**@b@	 * Return <code>true</code> if any element in '<code>candidates</code>' is@b@	 * contained in '<code>source</code>'; otherwise returns <code>false</code>.@b@	 * @param source the source Collection@b@	 * @param candidates the candidates to search for@b@	 * @return whether any of the candidates has been found@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static boolean containsAny( Collection source, Collection candidates) {@b@		if (isEmpty(source) || isEmpty(candidates)) {@b@			return false;@b@		}@b@		for (Object candidate : candidates) {@b@			if (source.contains(candidate)) {@b@				return true;@b@			}@b@		}@b@		return false;@b@	}@b@@b@	/**@b@	 * Return the first element in '<code>candidates</code>' that is contained in@b@	 * '<code>source</code>'. If no element in '<code>candidates</code>' is present in@b@	 * '<code>source</code>' returns <code>null</code>. Iteration order is@b@	 * {@link Collection} implementation specific.@b@	 * @param source the source Collection@b@	 * @param candidates the candidates to search for@b@	 * @return the first present object, or <code>null</code> if not found@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static Object findFirstMatch(Collection source, Collection candidates) {@b@		if (isEmpty(source) || isEmpty(candidates)) {@b@			return null;@b@		}@b@		for (Object candidate : candidates) {@b@			if (source.contains(candidate)) {@b@				return candidate;@b@			}@b@		}@b@		return null;@b@	}@b@@b@	/**@b@	 * Find a single value of the given type in the given Collection.@b@	 * @param collection the Collection to search@b@	 * @param type the type to look for@b@	 * @return a value of the given type found if there is a clear match,@b@	 * or <code>null</code> if none or more than one such value found@b@	 */@b@	@SuppressWarnings("unchecked")@b@	public static <T> T findValueOfType(Collection<?> collection, Class<T> type) {@b@		if (isEmpty(collection)) {@b@			return null;@b@		}@b@		T value = null;@b@		for (Object element : collection) {@b@			if (type == null || type.isInstance(element)) {@b@				if (value != null) {@b@					// More than one value found... no clear single value.@b@					return null;@b@				}@b@				value = (T) element;@b@			}@b@		}@b@		return value;@b@	}@b@@b@	/**@b@	 * Find a single value of one of the given types in the given Collection:@b@	 * searching the Collection for a value of the first type, then@b@	 * searching for a value of the second type, etc.@b@	 * @param collection the collection to search@b@	 * @param types the types to look for, in prioritized order@b@	 * @return a value of one of the given types found if there is a clear match,@b@	 * or <code>null</code> if none or more than one such value found@b@	 */@b@	public static Object findValueOfType(Collection<?> collection, Class<?>[] types) {@b@		if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {@b@			return null;@b@		}@b@		for (Class<?> type : types) {@b@			Object value = findValueOfType(collection, type);@b@			if (value != null) {@b@				return value;@b@			}@b@		}@b@		return null;@b@	}@b@@b@	/**@b@	 * Determine whether the given Collection only contains a single unique object.@b@	 * @param collection the Collection to check@b@	 * @return <code>true</code> if the collection contains a single reference or@b@	 * multiple references to the same instance, <code>false</code> else@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static boolean hasUniqueObject(Collection collection) {@b@		if (isEmpty(collection)) {@b@			return false;@b@		}@b@		boolean hasCandidate = false;@b@		Object candidate = null;@b@		for (Object elem : collection) {@b@			if (!hasCandidate) {@b@				hasCandidate = true;@b@				candidate = elem;@b@			}@b@			else if (candidate != elem) {@b@				return false;@b@			}@b@		}@b@		return true;@b@	}@b@@b@	/**@b@	 * Find the common element type of the given Collection, if any.@b@	 * @param collection the Collection to check@b@	 * @return the common element type, or <code>null</code> if no clear@b@	 * common type has been found (or the collection was empty)@b@	 */@b@	@SuppressWarnings("rawtypes")@b@	public static Class<?> findCommonElementType(Collection collection) {@b@		if (isEmpty(collection)) {@b@			return null;@b@		}@b@		Class<?> candidate = null;@b@		for (Object val : collection) {@b@			if (val != null) {@b@				if (candidate == null) {@b@					candidate = val.getClass();@b@				}@b@				else if (candidate != val.getClass()) {@b@					return null;@b@				}@b@			}@b@		}@b@		return candidate;@b@	}@b@@b@	/**@b@	 * Adapts an enumeration to an iterator.@b@	 * @param enumeration the enumeration@b@	 * @return the iterator@b@	 */@b@	public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {@b@		return new EnumerationIterator<E>(enumeration);@b@	}@b@@b@@b@	/**@b@	 * Iterator wrapping an Enumeration.@b@	 */@b@	private static class EnumerationIterator<E> implements Iterator<E> {@b@@b@		private Enumeration<E> enumeration;@b@@b@		public EnumerationIterator(Enumeration<E> enumeration) {@b@			this.enumeration = enumeration;@b@		}@b@@b@		public boolean hasNext() {@b@			return this.enumeration.hasMoreElements();@b@		}@b@@b@		public E next() {@b@			return this.enumeration.nextElement();@b@		}@b@@b@		public void remove() throws UnsupportedOperationException {@b@			throw new UnsupportedOperationException("Not supported");@b@		}@b@	}@b@@b@}