对List<实体类>重复值的操作
判断是否有重复:(Boolean)
public Boolean JudgeStopWatchValue(List<StopWatchValue> stopWatchValues) {
AtomicReference<Boolean> flag = new AtomicReference<>(true);
//判断码值编码是否重复
// 根据指定属性分组,并统计数量(key:指定属性,value:数量)
Map<Object, Long> mapGroup = stopWatchValues.stream().collect(Collectors.groupingBy(StopWatchValue::getcValue, Collectors.counting()));
// 筛选Map中value大于1的key
Stream<Object> stringStream = mapGroup.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey);
stringStream.forEach(str -> {
if (str!=null){
flag.set(false);
}
});
return flag.get();
}
查重:
public static void main(String[] args) {
Person person1 = new Person("小刘", 24, "男");
Person person2 = new Person("小贺", 18, "女");
Person person3 = new Person("小袁", 18, "女");
Person person4 = new Person("小李", 21, "男");
Person person5 = new Person("小子", 21, "男");
List<Person> persons = Arrays.asList(person1, person2, person3, person4,person5);
// 根据指定属性分组,并统计数量(key:指定属性,value:数量)
Map<Object, Long> mapGroup = persons.stream().collect(Collectors.groupingBy(person -> person.getAge(), Collectors.counting()));
System.out.println(mapGroup.toString());
// 筛选Map中value大于1的key
Stream<Object> stringStream = mapGroup.entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey());
System.out.print("重复的数据:{ ");
stringStream.forEach(str -> {
System.out.print(str + " ");
});
System.out.println("}");
}
去重:
public static void main(String[] args) {
Map map1=new HashMap();
Map map2=new HashMap();
Map map3=new HashMap();
map1.put("name","Jam");
map1.put("age","20");
map2.put("name","Jam");
map2.put("age","20");
map3.put("name","Jam");
map3.put("age","20");
List testList=new ArrayList();
testList.add(map1);
testList.add(map2);
testList.add(map2);
System.out.println("去重前");
System.out.println(testList);
//java8去重
testList= (List) testList.stream().distinct().collect(Collectors.toList());
System.out.println("去重后");
System.out.println(testList);
}
参考文章:List集合中对象属性判重_快了的码农的博客-CSDN博客_list判重
参考文章:如何优雅的判断List集合中对象的某个字段是否存在重复值_wanna do的博客-CSDN博客_判断list对象某个属性值是否重复
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/74746.html