1. import lombok.experimental.UtilityClass;
    2. import java.util.*;
    3. import java.util.concurrent.ConcurrentHashMap;
    4. import java.util.concurrent.ConcurrentMap;
    5. /**
    6. * Collection kit
    7. *
    8. * @author biezhi
    9. * @date 2017/12/15
    10. */
    11. @UtilityClass
    12. public class CollectionKit {
    13. /**
    14. * Determines whether an array is empty
    15. *
    16. * @param array array object
    17. * @param <T> array type
    18. * @return return array is empty
    19. */
    20. public static <T> boolean isEmpty(T[] array) {
    21. return null == array || array.length == 0;
    22. }
    23. /**
    24. * Determines whether an array is not empty
    25. *
    26. * @param array array object
    27. * @param <T> array type
    28. * @return return array is not empty
    29. */
    30. public static <T> boolean isNotEmpty(T[] array) {
    31. return null != array && array.length > 0;
    32. }
    33. /**
    34. * Determines whether an collection is empty
    35. *
    36. * @param collection collection object
    37. * @param <T> collection type
    38. * @return return collection is empty
    39. */
    40. public static <T> boolean isEmpty(Collection<T> collection) {
    41. return null == collection || collection.size() == 0;
    42. }
    43. /**
    44. * Determines whether an collection is not empty
    45. *
    46. * @param collection collection object
    47. * @param <T> collection type
    48. * @return return collection is not empty
    49. */
    50. public static <T> boolean isNotEmpty(Collection<T> collection) {
    51. return null != collection && collection.size() > 0;
    52. }
    53. /**
    54. * New HashMap
    55. *
    56. * @param <K> HashMap Key type
    57. * @param <V> HashMap Value type
    58. * @return return HashMap
    59. */
    60. public static <K, V> HashMap<K, V> newMap() {
    61. return new HashMap<>();
    62. }
    63. /**
    64. * New HashMap and initialCapacity
    65. *
    66. * @param initialCapacity initialCapacity
    67. * @param <K> HashMap Key type
    68. * @param <V> HashMap Value type
    69. * @return return HashMap
    70. */
    71. public static <K, V> HashMap<K, V> newMap(int initialCapacity) {
    72. return new HashMap<>(initialCapacity);
    73. }
    74. /**
    75. * New ConcurrentMap
    76. *
    77. * @param <K> ConcurrentMap Key type
    78. * @param <V> ConcurrentMap Value type
    79. * @return return ConcurrentMap
    80. */
    81. public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
    82. return new ConcurrentHashMap<>();
    83. }
    84. /**
    85. * New ConcurrentMap and initialCapacity
    86. *
    87. * @param initialCapacity initialCapacity
    88. * @param <K> ConcurrentMap Key type
    89. * @param <V> ConcurrentMap Value type
    90. * @return return ConcurrentMap
    91. */
    92. public static <K, V> ConcurrentMap<K, V> newConcurrentMap(int initialCapacity) {
    93. return new ConcurrentHashMap<>(initialCapacity);
    94. }
    95. /**
    96. * New List and add values
    97. *
    98. * @param values list values
    99. * @param <T> list type
    100. * @return return array list
    101. */
    102. @SafeVarargs
    103. public static <T> List<T> newLists(T... values) {
    104. if(null == values || values.length == 0){
    105. Assert.notNull(values, "values not is null.");
    106. }
    107. return Arrays.asList(values);
    108. }
    109. /**
    110. * New Set and add values
    111. *
    112. * @param values set values
    113. * @param <T> set type
    114. * @return return HashSet
    115. */
    116. @SafeVarargs
    117. public static <T> Set<T> newSets(T... values) {
    118. if(null == values || values.length == 0){
    119. Assert.notNull(values, "values not is null.");
    120. }
    121. return new HashSet<>(Arrays.asList(values));
    122. }
    123. }

    @UtilityClass解析:

    1. 标志该类是final的
    2. 不能声明任何构造方法,否则会报error。默认生成一个私有的构造方法。
    3. 类中所有的方法、成员变量、内部类都是static的。