去除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;
}
}
去除ArrayList集合中的重复自定义对象元素的更多相关文章
- Java 去除 ArrayList 集合中的重复元素
// One practice package Collection; import java.util.ArrayList; import java.util.Iterator; // 去除 Arr ...
- Java基础知识强化之集合框架笔记27:ArrayList集合练习之去除ArrayList集合中的重复字符串元素
1. 去除ArrayList集合中的重复字符串元素(字符串内容相同) 分析: (1)创建集合对象 (2)添加多个字符串元素(包含重复的) (3)创建新的集合 (4)遍历旧集合,获取得到每一个元素 (5 ...
- java集合 collection-list-ArrayList 去除ArrayList集合中的重复元素。
import java.util.*; /* 去除ArrayList集合中的重复元素. */ class ArrayListTest { public static void sop(Object o ...
- Java基础知识强化之集合框架笔记28:ArrayList集合练习之去除ArrayList集合中的重复字符串元素(升级)
1. 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) 要求:不能创建新的集合,就在以前的集合上做. 2. 代码示例之 去除集合中重复元素,不创建新的集合: package ...
- 集合框架-ArrayList练习(去除ArrayList集合中的重复元素)
1 package cn.itcast.p3.arraylist.test; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; ...
- 去除List集合中的重复值(四种好用的方法)(基本数据类型可用)
最近项目中需要对list集合中的重复值进行处理,大部分是采用两种方法,一种是用遍历list集合判断后赋给另一个list集合,一种是用赋给set集合再返回给list集合. 但是赋给set集合后,由于se ...
- 去除List集合中的重复值(四种好用的方法)
最近项目中需要对list集合中的重复值进行处理,大部分是采用两种方法,一种是用遍历list集合判断后赋给另一个list集合,一种是用赋给set集合再返回给list集合. 但是赋给set集合后,由于se ...
- Java去除ArrayList集合中重复字符串的案例
ArrayList去除集合中的字符串重复值 分析: A:创建集合对象 B:添加多个字符串元素 C:创建新集合 D:遍历旧集合,获取得到每一个元素 E:拿着个元素到新集合去找,看有没有 有:不进去 没有 ...
- 去除List集合中的重复对象,Map遍历代码
/*** * 去除List<PartsInfoDTO>列表中的重复对象 ~!! * @param list * @return */ public static List<Parts ...
随机推荐
- Awk 命令学习总结、AWk命令系列学习(linux shell)
AWK基本语法 下面没有提到awk命令怎么使用了,你可以通过 运行:awk –h 查询到所有命令及参数!下面把awk作为一门语言分节介绍. linux awk 内置变量使用介绍 awk语言中,怎么 ...
- CFtpFileFind例子
#include <afx.h> #include <afxwin.h> #include <afxinet.h> #include <stdio.h> ...
- struts2框架之拦截器(参考第二天学习笔记)
拦截器 1. 什么是拦截器 1). 与JavaWeb中的Filter比较相似. 2). 拦截器只能拦截Action!!! 2. Struts中定义了很多拦截器,其中defaultStack中的拦截器会 ...
- 博客主Judge已跳槽搬家emmm
跳槽网站:博客园 顺便带一下:洛谷blog (好久没更了QAQ...) ### 不过csdn上还是会照常更新的,毕竟用着方便
- v-on 绑定自定义事件
每个 Vue 实例都实现了事件接口,即: 使用 $on(eventName) 监听事件 使用 $emit(eventName) 触发事件 Vue 的事件系统与浏览器的 EventTarget API ...
- 004_wireshark专题
一.常用的wireshark搜索语法 (1) http.request.uri contains "admin/activities" #搜索URL包含"admin/ac ...
- ranlib 作用
ar 命令用于更新,维护管理静态库. ranlib 命令用于 更新库的符号索引表. 当只执行了ar命令(用于更新)时, ld连接时会仍然报错,查找不到更新的变量或函数,此时需要用ranlib来更新库的 ...
- Windows x64汇编函数调用约定
最近在写一些字符串函数的优化,用到x64汇编,我也是第一次接触,故跟大家分享一下. x86:又名 x32 ,表示 Intel x86 架构,即 Intel 的32位 80386 汇编指令集. x64: ...
- ansible笔记(3):ansible模块的基本使用
ansible笔记():ansible模块的基本使用 在前文的基础上,我们已经知道,当我们使用ansible完成实际任务时,需要依靠ansible的各个模块,比如,我们想要去ping某主机,则需要使用 ...
- Oracle 所有字典
select * from DBA_CONS_COLUMNS ; ---Information about accessible columns in constraint definitions s ...