Collections补充函数

This commit is contained in:
calvin.xiao
2015-11-29 02:45:35 +08:00
parent 9a54485adc
commit d64cc3ac8a
@@ -18,20 +18,125 @@ import org.apache.commons.lang3.StringUtils;
/**
* Collections工具集.
*
* 在JDK的Collections和Guava的Collections2后, 命名为Collections3.
* 主要由三部分组成:
*
* 函数主要由两部分组成,一是自反射提取元素的功能,二是源自Apache Commons Collection, 争取不用在项目里引入它。
* 1. 是否Empty的最常用函数
*
* 2. 源自Apache Commons Collection, 争取不用在项目里引入它。
*
* 3. 反射提取集合种元素及其属性的功能
*
* 在JDK的Collections和Guava的Collections2后, 命名为Collections3.
*
* 另请直接使用:
*
* JDK Collections的SingletonMap/SingletonList/EmptyMap/EmptyList/EmptyMap
* 1. JDK Collections的SingletonMap/SingletonList/EmptyMap/EmptyList/EmptyMap
*
* Guava Lists/Maps的newArrayLists(Element...elements)等函数
* 2. Guava Lists/Maps的newArrayLists(Element...elements)等函数
*
* @author calvin
*/
@SuppressWarnings("rawtypes")
public class Collections3 {
/**
* 判断是否为空.
*/
public static boolean isEmpty(Collection collection) {
return (collection == null) || collection.isEmpty();
}
/**
* 判断是否为空.
*/
public static boolean isEmpty(Map map) {
return (map == null) || map.isEmpty();
}
/**
* 判断是否不为空.
*/
public static boolean isNotEmpty(Collection collection) {
return (collection != null) && !(collection.isEmpty());
}
/**
* 判断是否不为空.
*/
public static boolean isNotEmpty(Map map) {
return (map != null) && !(map.isEmpty());
}
/**
* 取得Collection的第一个元素,如果collection为空返回null.
*/
public static <T> T getFirst(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
return collection.iterator().next();
}
/**
* 获取Collection的最后一个元素 ,如果collection为空返回null.
*/
public static <T> T getLast(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
// 当类型为array[]做内部结构的类型时,直接取得最后一个元素.
if (collection instanceof ArrayList) {
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.
*/
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.
*/
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.
*/
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;
}
/**
* 提取集合中的对象的两个属性(通过Getter函数), 组合成Map.
*
@@ -106,94 +211,4 @@ public class Collections3 {
return builder.toString();
}
/**
* 判断是否为空.
*/
public static boolean isEmpty(Collection collection) {
return (collection == null) || collection.isEmpty();
}
/**
* 判断是否为空.
*/
public static boolean isEmpty(Map map) {
return (map == null) || map.isEmpty();
}
/**
* 判断是否为空.
*/
public static boolean isNotEmpty(Collection collection) {
return (collection != null) && !(collection.isEmpty());
}
/**
* 取得Collection的第一个元素,如果collection为空返回null.
*/
public static <T> T getFirst(Collection<T> collection) {
if (isEmpty(collection)) {
return null;
}
return collection.iterator().next();
}
/**
* 获取Collection的最后一个元素 ,如果collection为空返回null.
*/
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.
*/
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.
*/
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.
*/
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;
}
}