list 中去除重复对象

需求:在list中对象A中元素a如果重复,视为2者相同,那么结果集如果需要去重。将对象A复写hashCode,equals a元素

package test;

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

public class T {

// 1351526433147036000
public static void main(String[] args) throws Exception {// 1351559547000
List fdf = new ArrayList();
fdf.add(new AAA("a"));
fdf.add(new AAA("a"));
fdf = new ArrayList(new LinkedHashSet(fdf));
System.out.println(fdf);

}
}

class CCC {
String d;

public int hashCode() {
return super.hashCode();
}

public boolean equals(Object obj) {
return super.equals(obj);
}
}

class AAA extends CCC {
String b;

public AAA(String b) {
this.b = b;
}

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((b == null) ? 0 : b.hashCode());
return result;
}

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AAA other = (AAA) obj;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
return true;
}

public String toString(){
return b;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值