ArrayList中元素去重问题
2016-01-25 14:46
405 查看
如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢?
代码实现:
辅助实体类:Student
测试类:
运行之后,发现打印结果并没有满足我们的要求,即去掉listA中重复的元素,这是怎么回事呢?
这是因为在使用List的contains() 方法判断是否包含一个元素时,需要把该元素与list中的所有元素进行比较,进而来确定是否包含这个元素。而上面的Student类中,并没有具体的实现equals方法,因而在比较的时候使用了Object的equals方法,故无法正确的比较,进而导致去重失败。
修改后的Student类:
再次运行测试类,去重成功。
这里我们使用HashSet ,HashSet的去重原理:
首先判断对象的hashcode值是否相同,如果相同,则再调用equals方法比较对象是否相等。如果相等,则说明元素重复,不添加到集合。
因此,使用Set的特性去重,Student类还必须重写Object 的hashcode方法。
实现代码:
Student类:
测试类:
思路一以及实现:
声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重之后数据 。遍历listA ,然后判断listB中是否包含各个元素,若不包含,把此元素加入到listB中。代码实现:
辅助实体类:Student
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.model.elgin.collections; public class Student { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(String id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "] " ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.model.elgin.collections; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class TestArrayList { public static void main(String[] args) { deleteRepeat1(); } /* * list去重1 * ArrayList元素的判断包含使用equals方法 */ public static void deleteRepeat1(){ List<Student> listA=new ArrayList<Student>(); List<Student> listB=new ArrayList<Student>(); Student st1=new Student("1","jack"); Student st2=new Student("2","jim"); Student st3=new Student("3","lucy"); Student st4=new Student("4","lily"); Student st5=new Student("4","lily"); listA.add(st1); listA.add(st2); listA.add(st3); listA.add(st4); listA.add(st5); for (Student student : listA) { if(!listB.contains(student)){ listB.add(student); } } System.out.println(listB); } } |
这是因为在使用List的contains() 方法判断是否包含一个元素时,需要把该元素与list中的所有元素进行比较,进而来确定是否包含这个元素。而上面的Student类中,并没有具体的实现equals方法,因而在比较的时候使用了Object的equals方法,故无法正确的比较,进而导致去重失败。
修改后的Student类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.model.elgin.collections; public class Student { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(String id, String name) { super(); this.id = id; this.name = name; } @Override public boolean equals(Object obj) { if(!(obj instanceof Student)){ return false; } Student student=(Student) obj; if(this.getId().equals(student.getId()) && this.getName().equals(student.getName())){ return true; } return false; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "] "; } } |
思路二以及实现:
我们知道,集合Set中的元素是唯一的,那么能否利用此特性,来完成list元素的去重呢。显然是可以的。这里我们使用HashSet ,HashSet的去重原理:
首先判断对象的hashcode值是否相同,如果相同,则再调用equals方法比较对象是否相等。如果相等,则说明元素重复,不添加到集合。
因此,使用Set的特性去重,Student类还必须重写Object 的hashcode方法。
实现代码:
Student类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.model.elgin.collections; public class Student { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(String id, String name) { super(); this.id = id; this.name = name; } @Override public boolean equals(Object obj) { if(!(obj instanceof Student)){ return false; } Student student=(Student) obj; if(this.getId().equals(student.getId()) && this.getName().equals(student.getName())){ return true; } return false; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "] "; } @Override public int hashCode() { return this.id.hashCode() + this.name.hashCode(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.model.elgin.collections; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class TestArrayList { public static void main(String[] args) { deleteRepeat2(); } /* * list去重2 * HashSet无序/唯一,判断包含使用hashcode、equals方法 */ public static void deleteRepeat2(){ List<Student> listA=new ArrayList<Student>(); Set<Student> listSet=new HashSet<Student>(); Student st1=new Student("1","jack"); Student st2=new Student("2","jim"); Student st3=new Student("3","lucy"); Student st4=new Student("4","lily"); Student st5=new Student("4","lily"); listA.add(st1); listA.add(st2); listA.add(st3); listA.add(st4); listA.add(st5); listSet.addAll(listA); System.out.println(listSet); } } |
相关文章推荐
- iOS重绘机制drawRect
- $.ajax返回不执行success的原因
- 只有五行的Floyd最短路算法
- hdu 4399 树形dp
- Linux c中一些常用函数总结(c语言中文网。。。)
- 第一天开始写博客
- JAVA JXL API的详细使用
- 组合模式
- Android开发工具全集
- hud-1754-I Hate It(线段树)
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
- loginUtil
- Visual Studio中的文件类型(sln vcproj suo user ncb)
- 八皇后问题及其推广
- 数字图像处理10--基于特征提取的拼接方法
- RHCE 学习笔记(12) 归档压缩和文件传递
- 周爱民:真正的架构师是没有title的(图灵访谈)
- 贪心算法,递归算法,动态规划算法比较与总结
- 机器学习算法之决策树(三)
- C#使用iTextSharp打印PDF