您的位置:首页 > 其它

List集合去除重复对象

2014-07-03 17:05 288 查看
大家好,我是一网菜鸟网(http://www.sklll.com)站长,网站目前正在维护中,今天在这里做一下List集合去除重复对象的简单文章说明,大家有什么不懂的可以给我留言或者到我群(225223397)内一起导论,我邮箱sklll@126.com。

一般初级程序员老是会遇到2个问题,一个是排序,一个是去除重复,去除重复最头疼的一般的数组什么的比较简单,但是对象的话很多初级程序员就束手无策了。这里我们以实例来讲解去除重复对象的灵活使用方法。

关于集合对象排序,前面已经讲过了,这里就不说了,集合对象去除重复主要是重写hashCode和equals方法。下面举例说明:

(一)实体类Cat.java

package com.skl.java.bean;
/**
*
* @author 沈坤林
* @date 2014年7月3日
*
*/
public class CatInfo {

private String catNo;

private String catName;

private String catSex;

private int catAge;

public CatInfo(String catNo, String catName, String catSex, int catAge) {
super();
this.catNo = catNo;
this.catName = catName;
this.catSex = catSex;
this.catAge = catAge;
}

public String getCatNo() {
return catNo;
}

public void setCatNo(String catNo) {
this.catNo = catNo;
}

public String getCatName() {
return catName;
}

public void setCatName(String catName) {
this.catName = catName;
}

public String getCatSex() {
return catSex;
}

public void setCatSex(String catSex) {
this.catSex = catSex;
}

public int getCatAge() {
return catAge;
}

public void setCatAge(int catAge) {
this.catAge = catAge;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + catAge;
result = prime * result + ((catName == null) ? 0 : catName.hashCode());
result = prime * result + ((catNo == null) ? 0 : catNo.hashCode());
result = prime * result + ((catSex == null) ? 0 : catSex.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CatInfo other = (CatInfo) obj;
if (catAge != other.catAge)
return false;
if (catName == null) {
if (other.catName != null)
return false;
} else if (!catName.equals(other.catName))
return false;
if (catNo == null) {
if (other.catNo != null)
return false;
} else if (!catNo.equals(other.catNo))
return false;
if (catSex == null) {
if (other.catSex != null)
return false;
} else if (!catSex.equals(other.catSex))
return false;
return true;
}

}


(二)测试类

package com.skl.java.docall;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.skl.java.bean.CatInfo;
/****
*
* @author 沈坤林
* @date 2014年7月3日
*
*/
public class Test {

public static void main(String[] args) {
CatInfo c1 = new CatInfo("No.001","小红1","A",1);
CatInfo c2 = new CatInfo("No.002","小红2","A",9);
CatInfo c3 = new CatInfo("No.003","小红3","A",8);
CatInfo c4 = new CatInfo("No.004","小红4","A",5);
CatInfo c5 = new CatInfo("No.005","小红5","A",5);
CatInfo c6 = new CatInfo("No.005","小红5","B",5);
CatInfo c7 = new CatInfo("No.005","小红5","B",5);

List<CatInfo> cats = new ArrayList<CatInfo>();
cats.add(c1);
cats.add(c2);
cats.add(c3);
cats.add(c4);
cats.add(c5);
cats.add(c5);
cats.add(c5);
cats.add(c6);
cats.add(c7);

Set<CatInfo> set = new LinkedHashSet<CatInfo>();
set.addAll(cats);
cats.clear();
cats.addAll(set);
System.out.println(cats.size());

}

}
这里打印的结果会是6个而不是7个,因为最后一个重复了。这里就是完全匹配是否想通了,如果对象每个字段完全相同的话就会把重复的去掉。有时候这并不是我们想要的效果。比如数据发生错误的时候,有2条数据catNo是一样的,而其他的信息有了变化,而我们只需要根据catNo来判断,这时候怎么办,如下:

(一)实体类:Cat.java

package com.skl.java.bean;
/**
*
* @author 沈坤林
* @date 2014年7月3日
*
*/
public class CatInfo {

private String catNo;

private String catName;

private String catSex;

private int catAge;

public CatInfo(String catNo, String catName, String catSex, int catAge) {
super();
this.catNo = catNo;
this.catName = catName;
this.catSex = catSex;
this.catAge = catAge;
}

public String getCatNo() {
return catNo;
}

public void setCatNo(String catNo) {
this.catNo = catNo;
}

public String getCatName() {
return catName;
}

public void setCatName(String catName) {
this.catName = catName;
}

public String getCatSex() {
return catSex;
}

public void setCatSex(String catSex) {
this.catSex = catSex;
}

public int getCatAge() {
return catAge;
}

public void setCatAge(int catAge) {
this.catAge = catAge;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
//result = prime * result + catAge;
//result = prime * result + ((catName == null) ? 0 : catName.hashCode());
result = prime * result + ((catNo == null) ? 0 : catNo.hashCode());
//result = prime * result + ((catSex == null) ? 0 : catSex.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CatInfo other = (CatInfo) obj;
/*if (catAge != other.catAge)
return false;
if (catName == null) {
if (other.catName != null)
return false;
} else if (!catName.equals(other.catName))
return false;*/
if (catNo == null) {
if (other.catNo != null)
return false;
} else if (!catNo.equals(other.catNo))
return false;
/*if (catSex == null) {
if (other.catSex != null)
return false;
} else if (!catSex.equals(other.catSex))
return false;*/
return true;
}

}
这里你们会发现我们把其他属性都的对比都注释掉了,只有catNo没有去掉,Test类不变,运行结果是5,这就达到了我们的目的了。好了,List集合去除重复对象就这么简单了,更多内容关注个人网站,www.sklll.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: