MapUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.cooleshow.base.utils;
  2. import android.util.Pair;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.Hashtable;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9. import java.util.TreeMap;
  10. /**
  11. * <pre>
  12. * author: blankj
  13. * blog : http://blankj.com
  14. * time : 2019/08/12
  15. * desc : utils about map
  16. * </pre>
  17. */
  18. public class MapUtils {
  19. private MapUtils() {
  20. throw new UnsupportedOperationException("u can't instantiate me...");
  21. }
  22. /**
  23. * Returns a new read-only map with the specified contents, given as a list of pairs
  24. * where the first value is the key and the second is the value.
  25. *
  26. * @param pairs a list of pairs
  27. * @return a new read-only map with the specified contents
  28. */
  29. @SafeVarargs
  30. public static <K, V> Map<K, V> newUnmodifiableMap(final Pair<K, V>... pairs) {
  31. return Collections.unmodifiableMap(newHashMap(pairs));
  32. }
  33. @SafeVarargs
  34. public static <K, V> HashMap<K, V> newHashMap(final Pair<K, V>... pairs) {
  35. HashMap<K, V> map = new HashMap<>();
  36. if (pairs == null || pairs.length == 0) {
  37. return map;
  38. }
  39. for (Pair<K, V> pair : pairs) {
  40. if (pair == null) continue;
  41. map.put(pair.first, pair.second);
  42. }
  43. return map;
  44. }
  45. @SafeVarargs
  46. public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(final Pair<K, V>... pairs) {
  47. LinkedHashMap<K, V> map = new LinkedHashMap<>();
  48. if (pairs == null || pairs.length == 0) {
  49. return map;
  50. }
  51. for (Pair<K, V> pair : pairs) {
  52. if (pair == null) continue;
  53. map.put(pair.first, pair.second);
  54. }
  55. return map;
  56. }
  57. @SafeVarargs
  58. public static <K, V> TreeMap<K, V> newTreeMap(final Comparator<K> comparator,
  59. final Pair<K, V>... pairs) {
  60. if (comparator == null) {
  61. throw new IllegalArgumentException("comparator must not be null");
  62. }
  63. TreeMap<K, V> map = new TreeMap<>(comparator);
  64. if (pairs == null || pairs.length == 0) {
  65. return map;
  66. }
  67. for (Pair<K, V> pair : pairs) {
  68. if (pair == null) continue;
  69. map.put(pair.first, pair.second);
  70. }
  71. return map;
  72. }
  73. @SafeVarargs
  74. public static <K, V> Hashtable<K, V> newHashTable(final Pair<K, V>... pairs) {
  75. Hashtable<K, V> map = new Hashtable<>();
  76. if (pairs == null || pairs.length == 0) {
  77. return map;
  78. }
  79. for (Pair<K, V> pair : pairs) {
  80. if (pair == null) continue;
  81. map.put(pair.first, pair.second);
  82. }
  83. return map;
  84. }
  85. /**
  86. * Null-safe check if the specified map is empty.
  87. * <p>
  88. * Null returns true.
  89. *
  90. * @param map the map to check, may be null
  91. * @return true if empty or null
  92. */
  93. public static boolean isEmpty(Map map) {
  94. return map == null || map.size() == 0;
  95. }
  96. /**
  97. * Null-safe check if the specified map is not empty.
  98. * <p>
  99. * Null returns false.
  100. *
  101. * @param map the map to check, may be null
  102. * @return true if non-null and non-empty
  103. */
  104. public static boolean isNotEmpty(Map map) {
  105. return !isEmpty(map);
  106. }
  107. /**
  108. * Gets the size of the map specified.
  109. *
  110. * @param map The map.
  111. * @return the size of the map specified
  112. */
  113. public static int size(Map map) {
  114. if (map == null) return 0;
  115. return map.size();
  116. }
  117. /**
  118. * Executes the given closure on each element in the collection.
  119. * <p>
  120. * If the input collection or closure is null, there is no change made.
  121. *
  122. * @param map the map to get the input from, may be null
  123. * @param closure the closure to perform, may be null
  124. */
  125. public static <K, V> void forAllDo(Map<K, V> map, Closure<K, V> closure) {
  126. if (map == null || closure == null) return;
  127. for (Map.Entry<K, V> kvEntry : map.entrySet()) {
  128. closure.execute(kvEntry.getKey(), kvEntry.getValue());
  129. }
  130. }
  131. /**
  132. * Transform the map by applying a Transformer to each element.
  133. * <p>
  134. * If the input map or transformer is null, there is no change made.
  135. *
  136. * @param map the map to get the input from, may be null
  137. * @param transformer the transformer to perform, may be null
  138. */
  139. public static <K1, V1, K2, V2> Map<K2, V2> transform(Map<K1, V1> map, final Transformer<K1, V1, K2, V2> transformer) {
  140. if (map == null || transformer == null) return null;
  141. try {
  142. final Map<K2, V2> transMap = map.getClass().newInstance();
  143. forAllDo(map, new Closure<K1, V1>() {
  144. @Override
  145. public void execute(K1 key, V1 value) {
  146. Pair<K2, V2> pair = transformer.transform(key, value);
  147. transMap.put(pair.first, pair.second);
  148. }
  149. });
  150. return transMap;
  151. } catch (IllegalAccessException e) {
  152. e.printStackTrace();
  153. } catch (InstantiationException e) {
  154. e.printStackTrace();
  155. }
  156. return null;
  157. }
  158. /**
  159. * Return the string of map.
  160. *
  161. * @param map The map.
  162. * @return the string of map
  163. */
  164. public static String toString(Map map) {
  165. if (map == null) return "null";
  166. return map.toString();
  167. }
  168. public interface Closure<K, V> {
  169. void execute(K key, V value);
  170. }
  171. public interface Transformer<K1, V1, K2, V2> {
  172. Pair<K2, V2> transform(K1 k1, V1 v1);
  173. }
  174. }