需求:在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;
}
}
list 中去除重复对象
猜你喜欢
转载自yr512656630.iteye.com/blog/1812294
今日推荐
周排行