com.github.dreamroute.common.util.CollectionUtil Maven / Gradle / Ivy
package com.github.dreamroute.common.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.Optional.ofNullable;
/**
* 描述:{@link java.util.Optional}辅助类
*
* @author w.dehi.2022-05-10
*/
public class CollectionUtil {
private CollectionUtil() {}
public static boolean isEmpty(Collection source) {
return source == null || source.isEmpty();
}
public static boolean isNotEmpty(Collection source) {
return !isEmpty(source);
}
public static boolean isEmpty(Map source) {
return source == null || source.isEmpty();
}
public static boolean isNotEmpty(Map source) {
return !isEmpty(source);
}
public static boolean isEmpty(T[] source) {
return source == null || source.length == 0 || Arrays.stream(source).allMatch(Objects::isNull);
}
public static boolean isNotEmpty(T[] source) {
return !isEmpty(source);
}
public static List nonNull(List source) {
return ofNullable(source).orElseGet(ArrayList::new);
}
public static Stream nonNullStream(List source) {
return nonNull(source).stream();
}
public static Set nonNull(Set source) {
return ofNullable(source).orElseGet(HashSet::new);
}
public static Stream nonNullStream(Set source) {
return nonNull(source).stream();
}
@SafeVarargs
public static List newArrayList(T... init) {
return new ArrayList<>(Arrays.asList(init));
}
public static List newArrayList(Collection extends T> c) {
return new ArrayList<>(c);
}
public static List newArrayListWithCapacity(int capacity) {
return new ArrayList<>(capacity);
}
}