123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package api.common.util;
- import java.util.*;
- public class CollectionUtil {
- //* -------------------------------- before jdk9 --------------------------------
- public static <T> List<T> arrayToList(T[] array) {
- return Arrays.asList(array);
- }
- public static <T> ArrayList<T> createArrayList(T... elements) {
- return new ArrayList<>(Arrays.asList(elements));
- }
- public static <T> HashSet<T> createHashSet(T... elements) {
- return new HashSet<>(Arrays.asList(elements));
- }
- public static <T> Set<T> listToSet(List<T> list) {
- return new HashSet<>(list);
- }
- public static <T> List<T> setToList(Set<T> set) {
- return new ArrayList<>(set);
- }
- 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 !isEmpty(collection);
- }
- public static boolean isNotEmpty(Map<?, ?> map) {
- return !isEmpty(map);
- }
- // //* -------------------------------- jdk9 --------------------------------
- //
- // /**
- // * 根据数组或参数序列创建只读的 list
- // *
- // * @param elements 数组或参数序列
- // * @param <T> 元素类型
- // * @return 只读 list
- // */
- // @SafeVarargs
- // public static <T> List<T> createUnmodifiableList(T... elements) {
- // return List.of(elements);
- // }
- //
- // /**
- // * 根据 list 创建只读的 list
- // *
- // * @param list list
- // * @param <E> 元素类型
- // * @return 只读 list
- // */
- // public static <E> List<E> createUnmodifiableList(List<E> list) {
- // return Collections.unmodifiableList(list);
- // }
- //
- // /**
- // * 根据 set 创建只读的 set
- // *
- // * @param set set
- // * @param <E> 元素类型
- // * @return 只读 list
- // */
- // public static <E> Set<E> createUnmodifiableList(Set<E> set) {
- // return Collections.unmodifiableSet(set);
- // }
- //
- //
- // /**
- // * 根据 map 创建只读的 map
- // *
- // * @param map map
- // * @param <K> 键类型
- // * @param <V> 值类型
- // * @return 只读 map
- // */
- // public static <K, V> Map<K, V> createUnmodifiableList(Map<K, V> map) {
- // return Collections.unmodifiableMap(map);
- // }
- }
|