com.fireflysource.common.collection.CollectionUtils Maven / Gradle / Ivy
package com.fireflysource.common.collection;
import java.util.*;
import java.util.stream.Collectors;
public class CollectionUtils {
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
public static boolean isEmpty(Map, ?> map) {
return (map == null || map.isEmpty());
}
public static boolean isEmpty(Collection> collection) {
return (collection == null || collection.isEmpty());
}
public static Set intersect(Set a, Set b) {
if (isEmpty(a) || isEmpty(b)) {
return new HashSet<>();
} else {
Set set = new HashSet<>(a);
set.retainAll(b);
return set;
}
}
public static Set union(Set a, Set b) {
Set set = new HashSet<>(a);
set.addAll(b);
return set;
}
public static boolean hasIntersection(Set a, Set b) {
if (isEmpty(a) || isEmpty(b)) {
return false;
}
if (a.size() < b.size()) {
if (a.size() < 8) {
return a.stream().anyMatch(b::contains);
} else {
return a.parallelStream().anyMatch(b::contains);
}
} else {
if (b.size() < 8) {
return b.stream().anyMatch(a::contains);
} else {
return b.parallelStream().anyMatch(a::contains);
}
}
}
@SafeVarargs
public static Set newHashSet(T... values) {
return Arrays.stream(values).collect(Collectors.toSet());
}
}