CollectionUtil.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package api.common.util;
  2. import java.util.*;
  3. public class CollectionUtil {
  4. //* -------------------------------- before jdk9 --------------------------------
  5. public static <T> List<T> arrayToList(T[] array) {
  6. return Arrays.asList(array);
  7. }
  8. public static <T> ArrayList<T> createArrayList(T... elements) {
  9. return new ArrayList<>(Arrays.asList(elements));
  10. }
  11. public static <T> HashSet<T> createHashSet(T... elements) {
  12. return new HashSet<>(Arrays.asList(elements));
  13. }
  14. public static boolean isEmpty(Collection<?> collection) {
  15. return collection == null || collection.isEmpty();
  16. }
  17. public static boolean isEmpty(Map<?, ?> map) {
  18. return map == null || map.isEmpty();
  19. }
  20. public static boolean isNotEmpty(Collection<?> collection) {
  21. return !isEmpty(collection);
  22. }
  23. public static boolean isNotEmpty(Map<?, ?> map) {
  24. return !isEmpty(map);
  25. }
  26. // //* -------------------------------- jdk9 --------------------------------
  27. //
  28. // /**
  29. // * 根据数组或参数序列创建只读的 list
  30. // *
  31. // * @param elements 数组或参数序列
  32. // * @param <T> 元素类型
  33. // * @return 只读 list
  34. // */
  35. // @SafeVarargs
  36. // public static <T> List<T> createUnmodifiableList(T... elements) {
  37. // return List.of(elements);
  38. // }
  39. //
  40. // /**
  41. // * 根据 list 创建只读的 list
  42. // *
  43. // * @param list list
  44. // * @param <E> 元素类型
  45. // * @return 只读 list
  46. // */
  47. // public static <E> List<E> createUnmodifiableList(List<E> list) {
  48. // return Collections.unmodifiableList(list);
  49. // }
  50. //
  51. // /**
  52. // * 根据 set 创建只读的 set
  53. // *
  54. // * @param set set
  55. // * @param <E> 元素类型
  56. // * @return 只读 list
  57. // */
  58. // public static <E> Set<E> createUnmodifiableList(Set<E> set) {
  59. // return Collections.unmodifiableSet(set);
  60. // }
  61. //
  62. //
  63. // /**
  64. // * 根据 map 创建只读的 map
  65. // *
  66. // * @param map map
  67. // * @param <K> 键类型
  68. // * @param <V> 值类型
  69. // * @return 只读 map
  70. // */
  71. // public static <K, V> Map<K, V> createUnmodifiableList(Map<K, V> map) {
  72. // return Collections.unmodifiableMap(map);
  73. // }
  74. }