1
2
3
4
5
6
7
8
9
10
11
12
13
|
Map<String, Long> key1key2CountMap = Optional.ofNullable(list).orElse(new ArrayList<>())
.stream().filter(Objects::nonNull)
.collect(groupingBy(s -> Pair.of(s.getKey1(), s.getKey2()), counting()));
// 查询两个key值都相同,出现的次数
Long count = Optional.of(key1key2CountMap).map(s -> Pair.of(s.getKey1(), s.getKey2())).orElse(0L);
// 判断查询两个key值都相同的数据,是否重复
key1key2CountMap.values().forEach(count -> {
if (count > 1) {
throw new RuntimeException("key1和key2都相同的数据,不能重复");
}
});
|