java如何从列表中删除重复项?
所以我有一个接口
public interface ARecord {
public BigInteger getAutoID();
public String getACompId();
}
及
public class APPPRecord extends AbstratAPRecord implements ARecord{
private BigInteger autoID;
private String ACompId = null;
//setter and getter
@Override
public int hashCode() {
return this.getACompId().hashCode();
}
@Override
public boolean equals(Object obj) {
APPPRecord e = null;
if(obj instanceof APPPRecord){
e = (APPPRecord) obj;
}
if(this.getACompId().equals(e.getACompId())){
return true;
}
else {
return false;
}
}
}
在职期间
List<APPPRecord> PFRecord = null;
while(scroll.next()) {
APPPRecord item = (APPPRecord) scroll.get(0);
List<ARecord> recs = new ArrayList<ARecord>();
recs.addAll(PFRecord);
我的PFRecord列表中有重复的结果。我必须使用能够检查ACompId包含密钥的哈希映射。如果密钥已经存在,不要将其传递给REC。阿道尔。我该怎么做呢?谢谢你的帮助
我尝试了Set,但仍然在我的模型类中看到HashCode()和equals()实现的重复结果
for (ARecord records:recs){
uniqueRecs.put(records.getACompId(), records);
Set<String> keys = uniqueRecs.keySet();
for(String key: keys){
log.debug("keys " + key);
}
}
还尝试了hashMaps
HashMap<String, ARecord > uniqueRecs = new HashMap<String, ARecord >();
for(ARecord records:recs){
if(!uniqueRecs.containsKey(records.getACompId())){
uniqueRecs.put(records.getACompId(), records);
for (String key : uniqueRecs.keySet()) {
log.debug("unique record " + key);
}
}
}
这两种方法仍然会产生重复的结果。有什么想法吗?我已经没有办法忽略这些复制品了
共 (0) 个答案