去除ArrayList集合中的重复自定义对象元素
要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复)。
思路:
1、创建一个新集合
2、遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在就添加到新集合中
Student类如下:有两个成员变量name和age
public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试类如下:
public class ListDemo { public static void main(String[] args) { //创建集合 ArrayList<Student> list = new ArrayList<Student>(); //创建集合元素对象 Student s1 = new Student("梨梨",21); Student s2 = new Student("熊熊",24); Student s3 = new Student("菜菜",10); Student s4 = new Student("梨梨",18); Student s5 = new Student("哈哈",25); Student s6 = new Student("熊熊",24); Student s7 = new Student("菜菜",10); //把对象添加到集合中 list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); list.add(s6); list.add(s7); //创建一个新集合 ArrayList<Student> newList = new ArrayList<Student>(); //遍历旧集合 Iterator it = list.iterator(); while(it.hasNext()){ Student s = (Student)it.next(); if(!newList.contains(s)){ newList.add(s); } } //遍历输出集合 for(int i = 0 ;i < newList.size();i++){ Student s = (Student)newList.get(i); System.out.println(s); } } }
输出结果为:
Student [name = 梨梨,age = 21] Student [name = 熊熊,age = 24] Student [name = 菜菜,age = 10] Student [name = 梨梨,age = 18] Student [name = 哈哈,age = 25] Student [name = 熊熊,age = 24] Student [name = 菜菜,age = 10]
看到输出结果,我们发现出错了。并没有去掉重复的对象元素。
通过查找代码错在哪里,我们可以发现可能在判断上面出了问题。判断新集合中是否存在已知元素,我们用了contains()方法。
我们看看contains方法的源码:
public boolean contains(Object o) { return indexOf(o) >= 0; } public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
我们可以发现,contains()方法的底层依赖的还是equals()方法,而在Student类中没有重写equals方法,因此调用的是Object的equals方法,而Object中的equals方法比较的是地址值,而通过new Student()创建的对象他们的地址值不可能一样,因此找到了问题所在。
要解决这问题,我们需要在Student中重写equals方法。
因此,只需要修改一下Student方法即可。
public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)