在實體對象中重寫equals方法: public class Tag implements Serializable{ /** * */ private static final long serialVersionUID = 8721123314415593476L; //主鍵ID private Integer tagId; //父類ID private Integer fatherId; //類型 private String tagType; //關鍵詞ID private Integer keywordId; //標籤名稱 private String tagName; //標籤等級 private Integer tagLevel; //建立時間 private Date createTime; //更新時間 private Date updateTime; public Integer getKeywordId() { return keywordId; } public void setKeywordId(Integer keywordId) { this.keywordId = keywordId; } public Integer getTagId() { return tagId; } public void setTagId(Integer tagId) { this.tagId = tagId; } public Integer getFatherId() { return fatherId; } public void setFatherId(Integer fatherId) { this.fatherId = fatherId; } public String getTagType() { return tagType; } public void setTagType(String tagType) { this.tagType = tagType; } public String getTagName() { return tagName; } public void setTagName(String tagName) { this.tagName = tagName; } public Integer getTagLevel() { return tagLevel; } public void setTagLevel(Integer tagLevel) { this.tagLevel = tagLevel; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } @Override public boolean equals(Object obj){ if(obj == null){ return false; }else { if(this.getClass() == obj.getClass()){ Tag tag = (Tag) obj; if(this.getTagId().equals(tag.getTagId())){ return true; }else{ return false; } }else{ return false; } } } 測試: public static void main(String[] args) { List<Tag> list = new ArrayList<Tag>(); Tag a = new Tag(); a.setTagId(123); list.add(a); Tag b = new Tag(); b.setTagId(123); System.out.println(list.contains(b)+"======="); } 結果:true=======