MapUtil.java
4.64 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
package com.yoho.unions.utils;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.function.BiConsumer;
/**
* map的工具类
* @author MALI
*/
public class MapUtil {
/**
* Iterator 转换成map
* @param values
* @param keyFunction
* @return
*/
public static final <K, V> Map<K, V> transformMap(Iterable<V> values, Function<? super V, K> keyFunction) {
if (Iterables.isEmpty(values) || null == keyFunction) {
return Maps.newHashMap();
}
Map<K, V> builder = new HashMap<K, V>();
Iterator<V> iterator = values.iterator();
while (iterator.hasNext()) {
V value = iterator.next();
builder.put(keyFunction.apply(value), value);
}
return builder;
}
/**
* Iterator 转换成<key,List>
* @param values
* @param keyFunction
* @return
*/
public static final <K, V> Map<K, List<V>> transformListMap(Iterable<V> values, Function<? super V, K> keyFunction) {
if (Iterables.isEmpty(values) || null == keyFunction) {
return Maps.newHashMap();
}
Map<K, List<V>> builder = new HashMap<K, List<V>>();
Iterator<V> iterator = values.iterator();
while (iterator.hasNext())
{
V value = iterator.next();
if(builder.containsKey(keyFunction.apply(value)))
{
builder.get(keyFunction.apply(value)).add(value);
}else
{
List<V> newArrayList=new ArrayList<V>();
newArrayList.add(value);
builder.put(keyFunction.apply(value), newArrayList);
}
}
return builder;
}
/**
* Iterator 转换成<key,List>
* @param values
* @param keyFunction
* @return
*/
public static final <V, T> Map<T, List<T>> transformListMap(Iterable<V> values, FunctionEx<V, T> keyFunction) {
if (Iterables.isEmpty(values) || null == keyFunction) {
return Maps.newHashMap();
}
Map<T, List<T>> builder = new HashMap<T, List<T>>();
Iterator<V> iterator = values.iterator();
while (iterator.hasNext())
{
V value = iterator.next();
if(builder.containsKey(keyFunction.applyKey(value)))
{
builder.get(keyFunction.applyKey(value)).add(keyFunction.applyValue(value));
}else
{
List<T> newArrayList=new ArrayList<T>();
newArrayList.add(keyFunction.applyValue(value));
builder.put(keyFunction.applyKey(value), newArrayList);
}
}
return builder;
}
/**
* Iterator 转换成<key,List>
* @param values
* @param keyFunction
* @return
*/
public static final <V, T, K> Map<T, K> transformListMap(Iterable<V> values, FunctionExPlus<V, T, K> keyFunction) {
if (Iterables.isEmpty(values) || null == keyFunction) {
return Maps.newHashMap();
}
Map<T, K> builder = new HashMap<T, K>();
Iterator<V> iterator = values.iterator();
while (iterator.hasNext())
{
V value = iterator.next();
builder.put(keyFunction.applyKey(value), keyFunction.applyValue(value));
}
return builder;
}
public static <T> List<T> getList(Map<Integer, List<T>> map)
{
if(MapUtils.isEmpty(map))
{
return Lists.newArrayList();
}
List<T> newArrayList = Lists.newArrayList();
for (List<T> item : map.values()) {
newArrayList.addAll(item);
}
return newArrayList;
}
/**
* 合并两个map,一个map的键值对应另一个map的值
* @param map1
* @param map2
* @return
*/
public static <K, T, V> Map<K, V> mergerTwoMap(Map<K, T> map1, Map<T, V> map2) {
if (MapUtils.isEmpty(map1) || MapUtils.isEmpty(map2)) {
return new HashMap<K, V>(0);
}
Map<K, V> result = new HashMap<K, V>(map1.size());
for (Map.Entry<K, T> entry : map1.entrySet()) {
result.put(entry.getKey(), map2.get(entry.getValue()));
}
return result;
}
public static interface Function<F, T> {
T apply(F input);
}
public static interface FunctionEx<F, T> {
T applyKey(F input);
T applyValue(F input);
}
public static interface FunctionExPlus<F, T, K> {
T applyKey(F input);
K applyValue(F input);
}
/**
* 删除不满足条件的
*/
public static <K, V> void filterMapByValue(Map<K, V> map, Predicate predicateValue) {
Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=it.next();
if (!predicateValue.evaluate(entry.getValue())) {
it.remove();
}
}
}
public static <K, V> void filterStringBlankMap(Map<K, V> map) {
filterMapByValue(map, o -> {
if (o == null) {
return false;
}
if (o instanceof String && StringUtils.isBlank((String)o)) {
return false;
}
return true;
});
}
}