CollectionUtil.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <T> Set<T> listToSet(List<T> list) {
  15. return new HashSet<>(list);
  16. }
  17. public static <T> List<T> setToList(Set<T> set) {
  18. return new ArrayList<>(set);
  19. }
  20. public static boolean isEmpty(Collection<?> collection) {
  21. return collection == null || collection.isEmpty();
  22. }
  23. public static boolean isEmpty(Map<?, ?> map) {
  24. return map == null || map.isEmpty();
  25. }
  26. public static boolean isNotEmpty(Collection<?> collection) {
  27. return !isEmpty(collection);
  28. }
  29. public static boolean isNotEmpty(Map<?, ?> map) {
  30. return !isEmpty(map);
  31. }
  32. // //* -------------------------------- jdk9 --------------------------------
  33. //
  34. // /**
  35. // * 根据数组或参数序列创建只读的 list
  36. // *
  37. // * @param elements 数组或参数序列
  38. // * @param <T> 元素类型
  39. // * @return 只读 list
  40. // */
  41. // @SafeVarargs
  42. // public static <T> List<T> createUnmodifiableList(T... elements) {
  43. // return List.of(elements);
  44. // }
  45. //
  46. // /**
  47. // * 根据 list 创建只读的 list
  48. // *
  49. // * @param list list
  50. // * @param <E> 元素类型
  51. // * @return 只读 list
  52. // */
  53. // public static <E> List<E> createUnmodifiableList(List<E> list) {
  54. // return Collections.unmodifiableList(list);
  55. // }
  56. //
  57. // /**
  58. // * 根据 set 创建只读的 set
  59. // *
  60. // * @param set set
  61. // * @param <E> 元素类型
  62. // * @return 只读 list
  63. // */
  64. // public static <E> Set<E> createUnmodifiableList(Set<E> set) {
  65. // return Collections.unmodifiableSet(set);
  66. // }
  67. //
  68. //
  69. // /**
  70. // * 根据 map 创建只读的 map
  71. // *
  72. // * @param map map
  73. // * @param <K> 键类型
  74. // * @param <V> 值类型
  75. // * @return 只读 map
  76. // */
  77. // public static <K, V> Map<K, V> createUnmodifiableList(Map<K, V> map) {
  78. // return Collections.unmodifiableMap(map);
  79. // }
  80. }