public final class CollectionUtil {
/**
* 创建一个<code>ArrayList</code>。
*/
public static <T> ArrayList<T> createArrayList() {
return new ArrayList<T>();
}
/**
* 创建一个<code>ArrayList</code>。
*/
public static <T> ArrayList<T> createArrayList(int initialCapacity) {
return new ArrayList<T>(initialCapacity);
}
/**
* 创建一个<code>ArrayList</code>。
*/
public static <T> ArrayList<T> createArrayList(Iterable<? extends T> c) {
ArrayList<T> list;
if (c instanceof Collection<?>) {
list = new ArrayList<T>((Collection<? extends T>) c);
} else {
list = new ArrayList<T>();
iterableToCollection(c, list);
list.trimToSize();
}
return list;
}
/**
* 创建一个<code>ArrayList</code>。
*/
public static <T, V extends T> ArrayList<T> createArrayList(V... args) {
if (args == null || args.length == 0) {
return new ArrayList<T>();
} else {
ArrayList<T> list = new ArrayList<T>(args.length);
for (V v : args) {
list.add(v);
}
return list;
}
}
/**
* 创建一个<code>LinkedList</code>。
*/
public static <T> LinkedList<T> createLinkedList() {
return new LinkedList<T>();
}
/**
* 创建一个<code>LinkedList</code>。
*/
public static <T> LinkedList<T> createLinkedList(Iterable<? extends T> c) {
LinkedList<T> list = new LinkedList<T>();
iterableToCollection(c, list);
return list;
}
/**
* 创建一个<code>LinkedList</code>。
*/
public static <T, V extends T> LinkedList<T> createLinkedList(V... args) {
LinkedList<T> list = new LinkedList<T>();
if (args != null) {
for (V v : args) {
list.add(v);
}
}
return list;
}
/**
* 创建一个<code>List</code>。
* <p>
* 和{@code createArrayList(args)}不同,本方法会返回一个不可变长度的列表,且性能高于
* {@code createArrayList(args)}。
* </p>
*/
public static <T> List<T> asList(T... args) {
if (args == null || args.length == 0) {
return Collections.emptyList();
} else {
return Arrays.asList(args);
}
}
/**
* 创建一个<code>HashMap</code>。
*/
public static <K, V> HashMap<K, V> createHashMap() {
return new HashMap<K, V>();
}
/**
* 创建一个<code>HashMap</code>。
*/
public static <K, V> HashMap<K, V> createHashMap(int initialCapacity) {
return new HashMap<K, V>(initialCapacity);
}
/**
* 创建一个<code>LinkedHashMap</code>。
*/
public static <K, V> LinkedHashMap<K, V> createLinkedHashMap() {
return new LinkedHashMap<K, V>();
}
/**
* 创建一个<code>LinkedHashMap</code>。
*/
public static <K, V> LinkedHashMap<K, V> createLinkedHashMap(int initialCapacity) {
return new LinkedHashMap<K, V>(initialCapacity);
}
/**
* 创建一个<code>TreeMap</code>。
*/
public static <K, V> TreeMap<K, V> createTreeMap() {
return new TreeMap<K, V>();
}
/**
* 创建一个<code>TreeMap</code>。
*/
public static <K, V> TreeMap<K, V> createTreeMap(Comparator<? super K> comparator) {
return new TreeMap<K, V>(comparator);
}
/**
* 创建一个<code>ConcurrentHashMap</code>。
*/
public static <K, V> ConcurrentHashMap<K, V> createConcurrentHashMap() {
return new ConcurrentHashMap<K, V>();
}
/**
* 创建一个<code>HashSet</code>。
*/
public static <T> HashSet<T> createHashSet() {
return new HashSet<T>();
}
/**
* 创建一个<code>HashSet</code>。
*/
public static <T, V extends T> HashSet<T> createHashSet(V... args) {
if (args == null || args.length == 0) {
return new HashSet<T>();
} else {
HashSet<T> set = new HashSet<T>(args.length);
for (V v : args) {
set.add(v);
}
return set;
}
}
/**
* 创建一个<code>HashSet</code>。
*/
public static <T> HashSet<T> createHashSet(Iterable<? extends T> c) {
HashSet<T> set;
if (c instanceof Collection<?>) {
set = new HashSet<T>((Collection<? extends T>) c);
} else {
set = new HashSet<T>();
iterableToCollection(c, set);
}
return set;
}
/**
* 创建一个<code>LinkedHashSet</code>。
*/
public static <T> LinkedHashSet<T> createLinkedHashSet() {
return new LinkedHashSet<T>();
}
/**
* 创建一个<code>LinkedHashSet</code>。
*/
public static <T, V extends T> LinkedHashSet<T> createLinkedHashSet(V... args) {
if (args == null || args.length == 0) {
return new LinkedHashSet<T>();
} else {
LinkedHashSet<T> set = new LinkedHashSet<T>(args.length);
for (V v : args) {
set.add(v);
}
return set;
}
}
/**
* 创建一个<code>LinkedHashSet</code>。
*/
public static <T> LinkedHashSet<T> createLinkedHashSet(Iterable<? extends T> c) {
LinkedHashSet<T> set;
if (c instanceof Collection<?>) {
set = new LinkedHashSet<T>((Collection<? extends T>) c);
} else {
set = new LinkedHashSet<T>();
iterableToCollection(c, set);
}
return set;
}
/**
* 创建一个<code>TreeSet</code>。
*/
public static <T> TreeSet<T> createTreeSet() {
return new TreeSet<T>();
}
/**
* 创建一个<code>TreeSet</code>。
*/
@SuppressWarnings("unchecked")
public static <T, V extends T> TreeSet<T> createTreeSet(V... args) {
return (TreeSet<T>) createTreeSet(null, args);
}
/**
* 创建一个<code>TreeSet</code>。
*/
public static <T> TreeSet<T> createTreeSet(Iterable<? extends T> c) {
return createTreeSet(null, c);
}
/**
* 创建一个<code>TreeSet</code>。
*/
public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator) {
return new TreeSet<T>(comparator);
}
/**
* 创建一个<code>TreeSet</code>。
*/
public static <T, V extends T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, V... args) {
TreeSet<T> set = new TreeSet<T>(comparator);
if (args != null) {
for (V v : args) {
set.add(v);
}
}
return set;
}
/**
* 创建一个<code>TreeSet</code>。
*/
public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, Iterable<? extends T> c) {
TreeSet<T> set = new TreeSet<T>(comparator);
iterableToCollection(c, set);
return set;
}
private static <T> void iterableToCollection(Iterable<? extends T> c, Collection<T> list) {
for (T element : c) {
list.add(element);
}
}
/**
* Return <code>true</code> if the supplied Collection is <code>null</code>
* or empty. Otherwise, return <code>false</code>.
*
* @param collection the Collection to check
* @return whether the given Collection is empty
*/
public static boolean isEmpty(Collection collection) {
return (collection == null || collection.isEmpty());
}
/**
* Return <code>false</code> if the supplied Collection is <code>null</code>
* or empty. Otherwise, return <code>true</code>.
*
* @param collection the Collection to check
* @return whether the given Collection is not empty
*/
public static boolean isNotEmpty(Collection collection) {
return !isEmpty(collection);
}
/**
* Return <code>true</code> if the supplied Map is <code>null</code>
* or empty. Otherwise, return <code>false</code>.
*
* @param map the Map to check
* @return whether the given Map is empty
*/
public static boolean isEmpty(Map map) {
return (map == null || map.isEmpty());
}
public static String[] toNoNullStringArray(Collection collection) {
if (collection == null) {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
return toNoNullStringArray(collection.toArray());
}
static String[] toNoNullStringArray(Object[] array) {
ArrayList list = new ArrayList(array.length);
for (int i = 0; i < array.length; i++) {
Object e = array[i];
if (e != null) {
list.add(e.toString());
}
}
return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
/////////////////////////////////
/**
* 提取集合中的对象的两个属性(通过Getter函数), 组合成Map.
*
* @param collection 来源集合.
* @param keyPropertyName 要提取为Map中的Key值的属性名.
* @param valuePropertyName 要提取为Map中的Value值的属性名.
*/
public static Map extractToMap(final Collection collection, final String keyPropertyName,
final String valuePropertyName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Map map = new HashMap(collection.size());
for (Object obj : collection) {
map.put(PropertyUtils.getProperty(obj, keyPropertyName),
PropertyUtils.getProperty(obj, valuePropertyName));
}
return map;
}
/**
* 提取集合中的对象的某个属性(通过Getter函数)作为key,集合对象作为值,组合成Map.
*
* @param collection 来源集合.
* @param keyPropertyName 要提取为Map中的Key值的属性名.
*/
public static <E> Map extractIndexToMap(final Collection<E> collection, final String keyPropertyName) {
if (collection == null) {
return new HashMap();
}
Map map = new HashMap(collection.size());
for (E obj : collection) {
if (obj == null) {
continue;
}
try {
map.put(PropertyUtils.getProperty(obj, keyPropertyName), obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成List.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
* @return
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static List extractToList(final Collection collection, final String propertyName) {
if (collection == null) {
return new ArrayList();
}
List list = new ArrayList(collection.size());
for (Object obj : collection) {
if (obj == null) {
continue;
}
try {
list.add(PropertyUtils.getProperty(obj, propertyName));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
* @param separator 分隔符.
* @return
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static String extractToString(final Collection collection, final String propertyName, final String separator) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
List list = extractToList(collection, propertyName);
return StringUtils.join(list, separator);
}
/**
* 转换Collection所有元素(通过toString())为String, 中间以 separator分隔。
*
* @param collection
* @param separator
* @return
*/
public static String convertToString(final Collection collection, final String separator) {
return StringUtils.join(collection, separator);
}
/**
* 转换Collection所有元素(通过toString())为String, 每个元素的前面加入prefix,后面加入postfix,如<div>mymessage</div>。
*
* @param collection
* @param prefix
* @param postfix
* @return
*/
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();
}
/**
* 取得Collection的第一个元素,如果collection为空返回null.
*
* @param collection
* @param <T>
* @return
*/
public static <T> T getFirst(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
return collection.iterator().next();
}
/**
* 获取Collection的最后一个元素 ,如果collection为空返回null.
*
* @param collection
* @param <T>
* @return
*/
public static <T> T getLast(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
//当类型为List时,直接取得最后一个元素 。
if (collection instanceof List) {
List<T> list = (List<T>) collection;
return list.get(list.size() - 1);
}
//其他类型通过iterator滚动到最后一个元素.
Iterator<T> iterator = collection.iterator();
while (true) {
T current = iterator.next();
if (!iterator.hasNext()) {
return current;
}
}
}
/**
* 返回a+b的新List.
*
* @param a
* @param b
* @param <T>
* @return
*/
public static <T> List<T> union(final Collection<T> a, final Collection<T> b) {
List<T> result = new ArrayList<T>(a);
result.addAll(b);
return result;
}
/**
* 返回a-b的新List.
*
* @param a
* @param b
* @param <T>
* @return
*/
public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
List<T> list = new ArrayList<T>(a);
for (T element : b) {
list.remove(element);
}
return list;
}
/**
* 返回a与b的交集的新List.
*
* @param a
* @param b
* @param <T>
* @return
*/
public static <T> List<T> intersection(Collection<T> a, Collection<T> b) {
List<T> list = new ArrayList<T>();
for (T element : a) {
if (b.contains(element)) {
list.add(element);
}
}
return list;
}
}
方便创建容器对象的工具CollectionUtil
最新推荐文章于 2024-12-13 16:37:57 发布
本文介绍了一个实用的集合操作工具类,提供了创建各种类型的集合、检查集合是否为空、从集合中提取属性形成新的集合或映射、转换集合元素为字符串等功能。通过这个工具类,可以简化集合操作,提高代码的可读性和维护性。
1166

被折叠的 条评论
为什么被折叠?



