import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;public class Demo {
public static void main(String[] args) {
Map<String, String> tempMap = new HashMap<>();
tempMap.put("aa", "1123");
tempMap.put("bb", "hello");
tempMap.put("cc", "hello");
tempMap.put("dd", "456");
tempMap.put("ee", "hello");
tempMap.put("ff", "1234555");
tempMap.put("gg", "456");
tempMap.put("hh", "aaaa");
tempMap.put("ii", "world");
tempMap.put("jj", "aaa");
List<String> valueList = tempMap.entrySet().stream().map(entry -> entry.getValue())
.collect(Collectors.groupingBy(s -> s, Collectors.counting()))
.entrySet().stream().filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey()).collect(Collectors.toList());
Set<String> valueSet = tempMap.entrySet().stream().map(entry -> entry.getValue())
.collect(Collectors.groupingBy(s -> s, Collectors.counting()))
.entrySet().stream().filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey()).collect(Collectors.toSet());
System.out.println(valueSet);
System.out.println(valueList);
}
}
取map里的重复value
Java Stream API实战:找出重复元素
最新推荐文章于 2022-11-09 17:23:40 发布
本文通过一个具体的Java代码示例,展示了如何使用Java Stream API来找出Map集合中重复出现的值,并将这些值收集到List和Set中。通过对Map的entrySet进行流操作,使用groupingBy和filter等方法,有效地处理了数据并识别出了重复项。

8223

被折叠的 条评论
为什么被折叠?



