CollectionUtil.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package api.common.util;
  2. import java.util.*;
  3. public class CollectionUtil {
  4. public static <T> List<T> arrayToList(T[] array) {
  5. return Arrays.asList(array);
  6. }
  7. @SafeVarargs
  8. public static <T> ArrayList<T> createArrayList(T... elements) {
  9. return new ArrayList<>(Arrays.asList(elements));
  10. }
  11. @SafeVarargs
  12. public static <T> HashSet<T> createHashSet(T... elements) {
  13. return new HashSet<>(Arrays.asList(elements));
  14. }
  15. public static <T> Set<T> listToSet(List<T> list) {
  16. return new HashSet<>(list);
  17. }
  18. public static <T> List<T> setToList(Set<T> set) {
  19. return new ArrayList<>(set);
  20. }
  21. public static boolean isEmpty(Collection<?> collection) {
  22. return collection == null || collection.isEmpty();
  23. }
  24. public static boolean isEmpty(ArrayList<?> arrayList) {
  25. return arrayList == null || arrayList.isEmpty();
  26. }
  27. public static boolean isEmpty(Map<?, ?> map) {
  28. return map == null || map.isEmpty();
  29. }
  30. public static boolean isNotEmpty(Collection<?> collection) {
  31. return !isEmpty(collection);
  32. }
  33. public static boolean isNotEmpty(Map<?, ?> map) {
  34. return !isEmpty(map);
  35. }
  36. public static void addValueToMap(Map<String, Integer> map, int value, String key) {
  37. map.merge(key, value, Integer::sum);
  38. }
  39. }