List集合去除重复对象
在java中,要将一个List中重复的对象除去,如果这个集合中的数据类型是基本数据类型,可以直接将List集合转换成Set,就会自动去除重复的元素,大家都知道Set集合的特点就是没有重复的,这个就相对比较简单,这里不在详细说,我们下面说的List集合中的数据类型是一个对象类型的情况。
当List集合中存储的类型是对象类型的时候,我们就不能简单的只把List集合转换成Set集合就行了,这时我们需要在对象的实体类中去重写equals()方法和hashCode()方法。我们以一个List集合为例,在该例中,我们将People实体类中姓名和电话号码作为判断该对象重复的标识,在People的实体类中我们重写这两个方法如下:
public class People {
private String name;
private int id;
private String phoneNumber;
private int age;
private String introduce;
public People(String name, int id, String phoneNumber, int age,
String introduce) {
super();
this.name = name;
this.id = id;
this.phoneNumber = phoneNumber;
this.age = age;
this.introduce = introduce;
}
// ....... 这里省略getter和setter方法
@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
People p = (People) arg0;
return name.equals(p.name) && phoneNumber.equals(p.phoneNumber);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
String str = name + phoneNumber;
return str.hashCode();
}
}
以上实体类中,我们在equals()方法中取出该对象的name和phoneNumber这两个属性值去判断比较,然后在重写的hashCode()方法中返回这两个属性值得hashCode值。
public class Test {
public static void main(String[] args) {
List<People> listPeople = new ArrayList<People>();
listPeople.add(new People("张三", 1, "13355556666", 23, "新员工"));
listPeople.add(new People("张三", 2, "15522223333", 23, "老员工"));
listPeople.add(new People("李四", 3, "13355556666", 23, "实习生"));
listPeople.add(new People("提莫", 4, "13311112222", 23, "经理"));
listPeople.add(new People("张三", 5, "13355556666", 23, "会计"));
listPeople.add(new People("德玛", 6, "3344", 23, "开发"));
listPeople.add(new People("卡特", 7, "13355556666", 23, "测试"));
listPeople.add(new People("提莫", 8, "13355556666", 23, "美工"));
listPeople.add(new People("提莫", 9, "13311112222", 23, "实施"));
listPeople.add(new People("卡兹克", 10, "13356786666", 23, "售前"));
listPeople.add(new People("亚索", 11, "13355556666", 23, "销售"));
Set<People> setData = new HashSet<People>();
setData.addAll(listPeople);
System.out.println("list- size----" + listPeople.size());
System.out.println("list-----" + listPeople.toString());
System.out.println("set- size----" + setData.size());
System.out.println("set-----" + setData.toString());
for(People pp : setData) {
System.out.println("p--" + pp.toString());
}
}
}
运行这段代码之后,我们就会发现,在原来的List集合中姓名和电话号码都相同的对象就被会认为是重复的元素而删除掉,很明显运行结果已经达到我们的目的。
这里需要说一下equals()方法和hashCode()方法,一般情况下我们重写equals()方法的时候都要去重写hashCode()方法,这是为什么呢?大家不妨可以这样去试一试上面那个例子,在实体类中将重写的hashCode()方法注释掉,再去运行该程序,这时就会发现运行结果并不是我们刚刚得到的结果,在Set集合中,并没有将我们认为是重复的元素删除掉。
还没有评论,来说两句吧...