
org.tinygroup.commons.tools.CollectionUtil Maven / Gradle / Ivy
/**
* Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tinygroup.commons.tools;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 方便创建容器对象的工具。
*
* @author renhui
*/
public final class CollectionUtil {
/**
* 创建一个ArrayList
。
*/
public static ArrayList createArrayList() {
return new ArrayList();
}
/**
* 创建一个ArrayList
。
*/
public static ArrayList createArrayList(int initialCapacity) {
return new ArrayList(initialCapacity);
}
/**
* 创建一个ArrayList
。
*/
public static ArrayList createArrayList(Iterable extends T> c) {
ArrayList list;
if (c instanceof Collection>) {
list = new ArrayList((Collection extends T>) c);
} else {
list = new ArrayList();
iterableToCollection(c, list);
list.trimToSize();
}
return list;
}
/**
* 创建一个ArrayList
。
*/
public static ArrayList createArrayList(V... args) {
if (args == null || args.length == 0) {
return new ArrayList();
} else {
ArrayList list = new ArrayList(args.length);
for (V v : args) {
list.add(v);
}
return list;
}
}
/**
* 创建一个LinkedList
。
*/
public static LinkedList createLinkedList() {
return new LinkedList();
}
/**
* 创建一个LinkedList
。
*/
public static LinkedList createLinkedList(Iterable extends T> c) {
LinkedList list = new LinkedList();
iterableToCollection(c, list);
return list;
}
/**
* 创建一个LinkedList
。
*/
public static LinkedList createLinkedList(V... args) {
LinkedList list = new LinkedList();
if (args != null) {
for (V v : args) {
list.add(v);
}
}
return list;
}
/**
* 创建一个List
。
*
* 和{@code createArrayList(args)}不同,本方法会返回一个不可变长度的列表,且性能高于
* {@code createArrayList(args)}。
*
*/
public static List asList(T... args) {
if (args == null || args.length == 0) {
return Collections.emptyList();
} else {
return Arrays.asList(args);
}
}
/**
* 创建一个HashMap
。
*/
public static HashMap createHashMap() {
return new HashMap();
}
/**
* 创建一个HashMap
。
*/
public static HashMap createHashMap(int initialCapacity) {
return new HashMap(initialCapacity);
}
/**
* 创建一个LinkedHashMap
。
*/
public static LinkedHashMap createLinkedHashMap() {
return new LinkedHashMap();
}
/**
* 创建一个LinkedHashMap
。
*/
public static LinkedHashMap createLinkedHashMap(int initialCapacity) {
return new LinkedHashMap(initialCapacity);
}
/**
* 创建一个TreeMap
。
*/
public static TreeMap createTreeMap() {
return new TreeMap();
}
/**
* 创建一个TreeMap
。
*/
public static TreeMap createTreeMap(Comparator super K> comparator) {
return new TreeMap(comparator);
}
/**
* 创建一个ConcurrentHashMap
。
*/
public static ConcurrentHashMap createConcurrentHashMap() {
return new ConcurrentHashMap();
}
/**
* 创建一个HashSet
。
*/
public static HashSet createHashSet() {
return new HashSet();
}
/**
* 创建一个HashSet
。
*/
public static HashSet createHashSet(V... args) {
if (args == null || args.length == 0) {
return new HashSet();
} else {
HashSet set = new HashSet(args.length);
for (V v : args) {
set.add(v);
}
return set;
}
}
/**
* 创建一个HashSet
。
*/
public static HashSet createHashSet(Iterable extends T> c) {
HashSet set;
if (c instanceof Collection>) {
set = new HashSet((Collection extends T>) c);
} else {
set = new HashSet();
iterableToCollection(c, set);
}
return set;
}
/**
* 创建一个LinkedHashSet
。
*/
public static LinkedHashSet createLinkedHashSet() {
return new LinkedHashSet();
}
/**
* 创建一个LinkedHashSet
。
*/
public static LinkedHashSet createLinkedHashSet(V... args) {
if (args == null || args.length == 0) {
return new LinkedHashSet();
} else {
LinkedHashSet set = new LinkedHashSet(args.length);
for (V v : args) {
set.add(v);
}
return set;
}
}
/**
* 创建一个LinkedHashSet
。
*/
public static LinkedHashSet createLinkedHashSet(Iterable extends T> c) {
LinkedHashSet set;
if (c instanceof Collection>) {
set = new LinkedHashSet((Collection extends T>) c);
} else {
set = new LinkedHashSet();
iterableToCollection(c, set);
}
return set;
}
/**
* 创建一个TreeSet
。
*/
public static TreeSet createTreeSet() {
return new TreeSet();
}
/**
* 创建一个TreeSet
。
*/
@SuppressWarnings("unchecked")
public static TreeSet createTreeSet(V... args) {
return (TreeSet) createTreeSet(null, args);
}
/**
* 创建一个TreeSet
。
*/
public static TreeSet createTreeSet(Iterable extends T> c) {
return createTreeSet(null, c);
}
/**
* 创建一个TreeSet
。
*/
public static TreeSet createTreeSet(Comparator super T> comparator) {
return new TreeSet(comparator);
}
/**
* 创建一个TreeSet
。
*/
public static TreeSet createTreeSet(Comparator super T> comparator, V... args) {
TreeSet set = new TreeSet(comparator);
if (args != null) {
for (V v : args) {
set.add(v);
}
}
return set;
}
/**
* 创建一个TreeSet
。
*/
public static TreeSet createTreeSet(Comparator super T> comparator, Iterable extends T> c) {
TreeSet set = new TreeSet(comparator);
iterableToCollection(c, set);
return set;
}
private static void iterableToCollection(Iterable extends T> c, Collection list) {
for (T element : c) {
list.add(element);
}
}
/**
* Return true
if the supplied Collection is null
* or empty. Otherwise, return false
.
*
* @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 true
if the supplied Map is null
* or empty. Otherwise, return false
.
*
* @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 Map extractIndexToMap(final Collection collection, final String keyPropertyName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Map map = new HashMap(collection.size());
for (Object obj : collection) {
map.put(PropertyUtils.getProperty(obj, keyPropertyName), obj);
}
return map;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成List.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
*/
public static List extractToList(final Collection collection, final String propertyName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
List list = new ArrayList(collection.size());
for (Object obj : collection) {
list.add(PropertyUtils.getProperty(obj, propertyName));
}
return list;
}
/**
* 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串.
*
* @param collection 来源集合.
* @param propertyName 要提取的属性名.
* @param separator 分隔符.
*/
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分隔。
*/
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();
}
/**
* 取得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;
}
}