//将多个Map组成的数组用每个Map中的相同key值进行分组,该方法也可传入一个Map,给一个Map中的元素进行分组
public static Map mapCombine(List<Map> list) {
Map<Object, List> map = new HashMap<>();
//遍历list
for (Map m : list) {
//将每个Map中的所有键存入到set集合中。因为set具备迭代器。所以可以迭代方式取出所有的键
Iterator<Object> it = m.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
//若new的Map中不存在某个key,则将该key对应的value存入list中并将此list存放进new的 Map中,key不变
if (!map.containsKey(key)) {
List newList = new ArrayList<>();
newList.add(m.get(key));
map.put(key, newList);
} else {
//若存在某个key则直接将其value值add进该key对应的list中
map.get(key).add(m.get(key));
}
}
}
return map;
}