-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMapUtils.java
More file actions
191 lines (174 loc) · 5.66 KB
/
MapUtils.java
File metadata and controls
191 lines (174 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.xzy.utils.map;
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;
/**
* Map 相关的工具类。
* 参考 https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/
* java/com/blankj/utilcode/util/MapUtils.java
*
* @author xzy
*/
@SuppressWarnings("unused")
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 <K, V> Map<K, V> newUnmodifiableMap(final Pair<K, V>... pairs) {
return Collections.unmodifiableMap(newHashMap(pairs));
}
@SafeVarargs
public static <K, V> HashMap<K, V> newHashMap(final Pair<K, V>... pairs) {
HashMap<K, V> map = new HashMap<>();
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
@SafeVarargs
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(final Pair<K, V>... pairs) {
LinkedHashMap<K, V> map = new LinkedHashMap<>();
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
@SafeVarargs
public static <K, V> TreeMap<K, V> newTreeMap(final Comparator<K> comparator,
final Pair<K, V>... pairs) {
if (comparator == null) {
throw new IllegalArgumentException("comparator must not be null");
}
TreeMap<K, V> map = new TreeMap<>(comparator);
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
@SafeVarargs
public static <K, V> Hashtable<K, V> newHashTable(final Pair<K, V>... pairs) {
Hashtable<K, V> map = new Hashtable<>();
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
/**
* Null-safe check if the specified map is empty.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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 <K, V> void forAllDo(Map<K, V> map, Closure<K, V> closure) {
if (map == null || closure == null) return;
for (Map.Entry<K, V> kvEntry : map.entrySet()) {
closure.execute(kvEntry.getKey(), kvEntry.getValue());
}
}
/**
* Transform the map by applying a Transformer to each element.
* <p>
* 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 <K1, V1, K2, V2> Map<K2, V2> transform(Map<K1, V1> map, final Transformer<K1, V1, K2, V2> transformer) {
if (map == null || transformer == null) return null;
try {
final Map<K2, V2> transMap = map.getClass().newInstance();
forAllDo(map, new Closure<K1, V1>() {
@Override
public void execute(K1 key, V1 value) {
Pair<K2, V2> 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<K, V> {
void execute(K key, V value);
}
public interface Transformer<K1, V1, K2, V2> {
Pair<K2, V2> transform(K1 k1, V1 v1);
}
}