CollectionUtil.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package api.common.util;
  2. import java.util.*;
  3. public class CollectionUtil {
  4. public static <E> E[] createArray(E[] elements) {
  5. return elements;
  6. }
  7. public static int[] createIntArray(int... elements) {
  8. return elements;
  9. }
  10. public static <T> List<T> arrayToList(T[] array) {
  11. return Arrays.asList(array);
  12. }
  13. @SafeVarargs
  14. public static <T> ArrayList<T> createArrayList(T... elements) {
  15. return new ArrayList<>(Arrays.asList(elements));
  16. }
  17. @SafeVarargs
  18. public static <T> HashSet<T> createHashSet(T... elements) {
  19. return new HashSet<>(Arrays.asList(elements));
  20. }
  21. public static <T> Set<T> listToSet(List<T> list) {
  22. return new HashSet<>(list);
  23. }
  24. public static <E> String listToSequence(List<E> list) {
  25. StringBuilder stringBuilder = new StringBuilder();
  26. list.forEach(element -> stringBuilder.append(element).append(","));
  27. if (list.size() != 0) {
  28. stringBuilder.deleteCharAt(stringBuilder.length() - 1);
  29. }
  30. return stringBuilder.toString();
  31. }
  32. public static List<String> sequenceStringToList(String sequenceString, String regex) {
  33. return Arrays.asList(sequenceString.split(regex));
  34. }
  35. public static <T> List<T> setToList(Set<T> set) {
  36. return new ArrayList<>(set);
  37. }
  38. public static boolean isEmpty(Collection<?> collection) {
  39. return collection == null || collection.isEmpty();
  40. }
  41. public static boolean isEmpty(ArrayList<?> arrayList) {
  42. return arrayList == null || arrayList.isEmpty();
  43. }
  44. public static boolean isEmpty(Map<?, ?> map) {
  45. return map == null || map.isEmpty();
  46. }
  47. public static boolean isNotEmpty(Collection<?> collection) {
  48. return !isEmpty(collection);
  49. }
  50. public static boolean isNotEmpty(Map<?, ?> map) {
  51. return !isEmpty(map);
  52. }
  53. public static void addValueToMap(Map<String, Integer> map, int value, String key) {
  54. map.merge(key, value, Integer::sum);
  55. }
  56. }