package com.test;import java.math.BigDecimal;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import org.apache.commons.collections.CollectionUtils;public class TestCollection { //去重复之前集合 private static List<User> list = Arrays.asList( new User("张三", BigDecimal.valueOf(35.6), 18), new User("李四", BigDecimal.valueOf(85), 30), new User("赵六", BigDecimal.valueOf(66.55), 25), new User("赵六", BigDecimal.valueOf(66.55), 25), new User("张三", BigDecimal.valueOf(35.6), 18)); public static void main(String[] args) { //排除重复 getNoRepeatList(list); } /** * 去除List内复杂字段重复对象 * @param oldList * @return */ private static List<User> getNoRepeatList(List<User> oldList){ List<User> list = new ArrayList<>(); if(CollectionUtils.isNotEmpty(oldList)){ for (User user : oldList) { //list去重复,内部重写equals if(!list.contains(user)){ list.add(user); } } } //输出新集合 System.out.println("去除重复后新集合:"); if(CollectionUtils.isNotEmpty(list)){ for (User user : list) { System.out.println(user.toString()); } } return list; } static class User{ private String userName; //姓名 private BigDecimal score;//分数 private Integer age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public BigDecimal getScore() { return score; } public void setScore(BigDecimal score) { this.score = score; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String userName, BigDecimal score, Integer age) { super(); this.userName = userName; this.score = score; this.age = age; } public User() { // TODO Auto-generated constructor stub } @Override public String toString() { // TODO Auto-generated method stub return "姓名:"+ this.userName + ",年龄:" + this.age + ",分数:" + this.score; } /** * 重写equals,用于比较对象属性是否包含 */ public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } User user = (User) obj; //多重逻辑处理,去除年龄、姓名相同的记录 if (this.getAge() .compareTo(user.getAge())==0 && this.getUserName().equals(user.getUserName()) && this.getScore().compareTo(user.getScore())==0) { return true; } return false; } }}