com.star.collection.CollectionUtil Maven / Gradle / Ivy
package com.star.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import com.star.lang.Filter;
/**
* 集合工具类
*
* @author http://git.oschina.net/loolly/hutool
*
*/
public final class CollectionUtil {
/**
* 构造方法
*/
private CollectionUtil() {
// 防止初始化
}
/**
* 以 conjunction 为分隔符将集合转换为字符串
*
* jdk1.8用stringjoiner
*/
public static String join(final Iterable collection, final char conjunction) {
final StringBuilder builder = new StringBuilder();
boolean isFirst = true;
for (final T item : collection) {
if (isFirst) {
isFirst = false;
} else {
builder.append(conjunction);
}
builder.append(item);
}
return builder.toString();
}
/**
*
* 集合是否为空
*
*/
public static boolean isEmpty(final Collection> collection) {
return collection == null || collection.isEmpty();
}
/**
*
* Map是否为空
*
*/
public static boolean isEmpty(final Map, ?> map) {
return map == null || map.isEmpty();
}
/**
* 初始化hashmap
*
* initialCapacity=0,默认initialCapacity>0,采用initialCapacity大小初始化
*/
public static Map getMap(final Integer initialCapacity) {
return Objects.isNull(initialCapacity) ? new HashMap<>() : new HashMap<>(initialCapacity);
}
/**
* 初始化ConcurrentHashMap
*
* initialCapacity=0,默认initialCapacity>0,采用initialCapacity大小初始化
*/
public static Map getConcurrentMap(final Integer initialCapacity) {
return Objects.isNull(initialCapacity) ? new ConcurrentHashMap()
: new ConcurrentHashMap(initialCapacity);
}
/**
* 初始化LinkedHashMap
*
* initialCapacity=0,默认initialCapacity>0,采用initialCapacity大小初始化
*/
public static Map getLinkedMap(final Integer initialCapacity) {
return Objects.isNull(initialCapacity) ? new LinkedHashMap() : new LinkedHashMap(initialCapacity);
}
/**
* 初始化TreeMap
*/
public static Map getTreeMap() {
return new TreeMap();
}
/**
* 初始化ArrayList
*
* initialCapacity=0,默认initialCapacity>0,采用initialCapacity大小初始化
*/
public static List getList(final Integer initialCapacity) {
return Objects.isNull(initialCapacity) ? new ArrayList() : new ArrayList(initialCapacity);
}
/**
* 初始化LinkedList
*/
public static List getLinkedList() {
return new LinkedList();
}
/**
* 初始化HashSet
*
* initialCapacity=0,默认initialCapacity>0,采用initialCapacity大小初始化
*/
public static Set getHashSet(final Integer initialCapacity) {
return Objects.isNull(initialCapacity) ? new HashSet() : new HashSet(initialCapacity);
}
/**
* 集合转成TreeSet
*/
public static Set getTreeSet() {
return new TreeSet();
}
/**
* 集合转成treeset
*/
public static Set getTreeSet(final Collection extends T> collection) {
return isEmpty(collection) ? new TreeSet<>() : new TreeSet<>(collection);
}
/**
* 用该方法来代替 {@code new LinkedList()} 方式获得新的 {@code java.util.Queue} 的实例对象。
*/
public static Queue getQueue() {
return new LinkedList();
}
/**
* 合并set
*/
public static Set unionHashSet(final Set setA, final Set setB) {
Set result;
if (isEmpty(setA) && isEmpty(setB)) {
result = Collections.emptySet();
} else {
result = wrapHashSet(setA);
result.addAll(setB);
}
return result;
}
/**
* 两个set取交集
*/
public static Set intersectHashSet(final Set setA, final Set setB) {
Set result;
if (isEmpty(setA) && isEmpty(setB)) {
result = Collections.emptySet();
} else {
result = wrapHashSet(setA);
result.retainAll(setB);
}
return result;
}
/**
* 在seta中删除和setb的交集
*/
public static Set differenceHashSet(final Set setA, final Set setB) {
Set result;
if (isEmpty(setA)) {
result = Collections.emptySet();
} else if (isEmpty(setB)) {
result = setA;
} else {
result = wrapHashSet(setA);
result.removeAll(setB);
}
return result;
}
/**
* 取seta和setb的补集
*/
public static Set complementHashSet(final Set setA, final Set setB) {
return differenceHashSet(unionHashSet(setA, setB), intersectHashSet(setA, setB));
}
/**
* 集合转成arraylist
*/
public static List wrapList(final Collection extends T> collection) {
return isEmpty(collection) ? new ArrayList() : new ArrayList(collection);
}
/**
* 集合转成hashset
*/
public static Set wrapHashSet(final Collection extends T> collection) {
return isEmpty(collection) ? new HashSet<>() : new HashSet<>(collection);
}
/**
*
* 新建一个HashSet
*
*/
@SafeVarargs
public static Set wrapHashSet(final T... instances) {
Set set;
if (ArrayUtil.isEmpty(instances)) {
set = Collections.emptySet();
} else {
set = new HashSet<>(instances.length);
for (final T instance : instances) {
set.add(instance);
}
}
return set;
}
/**
*
* 新建一个list
*
*/
@SafeVarargs
public static List wrapList(final T... instances) {
List list;
if (ArrayUtil.isEmpty(instances)) {
list = Collections.emptyList();
} else {
list = new ArrayList<>(instances.length);
for (final T instance : instances) {
list.add(instance);
}
}
return list;
}
/**
* 过滤集合
*
* java8考虑用流来处理
*
* 出于性能考虑,调用前确保collection是拷贝
*/
public static Collection filter(final Collection collection, final Filter filter) {
final Collection result = collection;
if (!Objects.isNull(filter)) {
result.clear();
for (final T instance : collection) {
if (filter.accept(instance)) {
result.add(instance);
}
}
}
return result;
}
/**
* 过滤map
*
* java8考虑用流来处理
*
* 出于性能考虑,调用前确保map是拷贝
*/
public static Map filter(final Map map, final Filter> filter) {
final Map result = map;
if (!Objects.isNull(filter)) {
result.clear();
for (final Entry entry : map.entrySet()) {
if (filter.accept(entry)) {
result.put(entry.getKey(), entry.getValue());
}
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy