1、 找出List对象集合某个字段的重复值
public static void main(String[] args) {
List<OrderViewModel> orderList = new ArrayList<>();
for (int i=0;i<10;i++){
OrderViewModel model = new OrderViewModel();
model.setPcno("1111"+i);
model.setProductQty(22+i);
orderList.add(model);
}
OrderViewModel model1 = new OrderViewModel();
model1.setPcno("11118");
model1.setProductQty(955);
orderList.add(model1);
List<String> list =orderList.stream().
collect(Collectors.groupingBy(OrderViewModel::getPcno,Collectors.counting()))
.entrySet().stream()
.filter(entry->entry.getValue()>1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if(list.size()>0){
System.out.println("groupingBy的字段有"+list.size()+"个重复,分别为:");
list.forEach(System.out::println);
}else{
System.out.println("groupingBy的字段没有重复。");
}
List<OrderViewModel> newOrder = removeDuplicateOrder(orderList);
System.out.println(newOrder);
}
2、对List对象集合某个字段进行排重
private static List<OrderViewModel> removeDuplicateOrder(List<OrderViewModel> orderList) {
Set<OrderViewModel> set = new TreeSet<>(Comparator.comparing(OrderViewModel::getPcno));
set.addAll(orderList);
return new ArrayList<>(set);
}