public static void main(String[] args) {
//1.创建原始数据
Map<String,String> map = new HashMap<>();
map.put("1","1");
map.put("2","1");
map.put("3","2");
map.put("4","3");
map.put("5","1");
//2.获取Value集合
Collection<String> values = map.values();
Set<String> set1 = new HashSet<>();
Iterator iterator = values.iterator();
while (iterator.hasNext()){
//3.把相同Value值放进Set中,利用Set的去重性把value进行分组
set1.add((String) iterator.next());
}
//4.创建list数组
List<Map<String,String>> arr = new ArrayList<>();
Iterator iterSet = set1.stream().iterator();
//5.遍历map与Set的value进行比对,相同的放进新建的Map中
while (iterSet.hasNext()) {
Map<String,String> tempMap = new HashMap<>();
String value = (String) iterSet.next();
for (String key: map.keySet()) {
String val = (String) map.get(key);
if (value.equals(val)) {
tempMap.put(key,value);
}
}
arr.add(tempMap);
}
//遍历结果集
arr.forEach(arg->{
System.out.println(arg);
});
}
代码执行后的结果集: