List对象集合根据指定属性去重

如题 为了记录删除List内重复数据方法

比较简单,只是重写了equals方法,直接贴代码
public class Demo {
    /**
     * 创建测试数据
     */
    private static List<User> list = Arrays.asList(
            new User("猪场", BigDecimal.valueOf(88.88), "英语"),
            new User("鹅厂", BigDecimal.valueOf(85), "数学"),
            new User("狗厂", BigDecimal.valueOf(77.99), "语文"),
            new User("狗厂", BigDecimal.valueOf(77.99), "语文"),
            new User("猪场", BigDecimal.valueOf(88.88), "英语"));

    public static void main(String[] args) {
        // 去重
        getDeleteRepeatList(list);
    }

    /**
     * 去除List内复杂字段重复对象
     * 
     * @param oldList
     * @return
     */
    private static List<User> getDeleteRepeatList(List<User> oldList){
        List<User> list = new ArrayList<User>();
        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 String name;
        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 String getAge() {
            return name;
        }
        public void setAge(String age) {
            this.name = name;
        }
        public User(String userName, BigDecimal score, String name) {
            super();
            this.userName = userName;
            this.score = score;
            this.name = name;
        }
        public User() {}

        @Override
        public String toString() {
            return "姓名:"+ this.userName + ",科目:" + this.name + ",分数:" + this.score;
        }

        /**
         * 重写equals方法
         */
        @Override
        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;
        }
    }
}
执行前数据

在这里插入图片描述

执行后数据

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑猪撞地球QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值