package com.cooleshow.base.utils; import android.util.Pair; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; /** *
 *     author: blankj
 *     blog  : http://blankj.com
 *     time  : 2019/08/12
 *     desc  : utils about map
 * 
*/ public class MapUtils { private MapUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * Returns a new read-only map with the specified contents, given as a list of pairs * where the first value is the key and the second is the value. * * @param pairs a list of pairs * @return a new read-only map with the specified contents */ @SafeVarargs public static Map newUnmodifiableMap(final Pair... pairs) { return Collections.unmodifiableMap(newHashMap(pairs)); } @SafeVarargs public static HashMap newHashMap(final Pair... pairs) { HashMap map = new HashMap<>(); if (pairs == null || pairs.length == 0) { return map; } for (Pair pair : pairs) { if (pair == null) continue; map.put(pair.first, pair.second); } return map; } @SafeVarargs public static LinkedHashMap newLinkedHashMap(final Pair... pairs) { LinkedHashMap map = new LinkedHashMap<>(); if (pairs == null || pairs.length == 0) { return map; } for (Pair pair : pairs) { if (pair == null) continue; map.put(pair.first, pair.second); } return map; } @SafeVarargs public static TreeMap newTreeMap(final Comparator comparator, final Pair... pairs) { if (comparator == null) { throw new IllegalArgumentException("comparator must not be null"); } TreeMap map = new TreeMap<>(comparator); if (pairs == null || pairs.length == 0) { return map; } for (Pair pair : pairs) { if (pair == null) continue; map.put(pair.first, pair.second); } return map; } @SafeVarargs public static Hashtable newHashTable(final Pair... pairs) { Hashtable map = new Hashtable<>(); if (pairs == null || pairs.length == 0) { return map; } for (Pair pair : pairs) { if (pair == null) continue; map.put(pair.first, pair.second); } return map; } /** * Null-safe check if the specified map is empty. *

* Null returns true. * * @param map the map to check, may be null * @return true if empty or null */ public static boolean isEmpty(Map map) { return map == null || map.size() == 0; } /** * Null-safe check if the specified map is not empty. *

* Null returns false. * * @param map the map to check, may be null * @return true if non-null and non-empty */ public static boolean isNotEmpty(Map map) { return !isEmpty(map); } /** * Gets the size of the map specified. * * @param map The map. * @return the size of the map specified */ public static int size(Map map) { if (map == null) return 0; return map.size(); } /** * Executes the given closure on each element in the collection. *

* If the input collection or closure is null, there is no change made. * * @param map the map to get the input from, may be null * @param closure the closure to perform, may be null */ public static void forAllDo(Map map, Closure closure) { if (map == null || closure == null) return; for (Map.Entry kvEntry : map.entrySet()) { closure.execute(kvEntry.getKey(), kvEntry.getValue()); } } /** * Transform the map by applying a Transformer to each element. *

* If the input map or transformer is null, there is no change made. * * @param map the map to get the input from, may be null * @param transformer the transformer to perform, may be null */ public static Map transform(Map map, final Transformer transformer) { if (map == null || transformer == null) return null; try { final Map transMap = map.getClass().newInstance(); forAllDo(map, new Closure() { @Override public void execute(K1 key, V1 value) { Pair pair = transformer.transform(key, value); transMap.put(pair.first, pair.second); } }); return transMap; } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return null; } /** * Return the string of map. * * @param map The map. * @return the string of map */ public static String toString(Map map) { if (map == null) return "null"; return map.toString(); } public interface Closure { void execute(K key, V value); } public interface Transformer { Pair transform(K1 k1, V1 v1); } }