有個自訂類別
public class Node {
public Node(String name,String type){
this.name = name;
this.type = type;}
public int hashCode() {
int result;
result = this.name.hashCode();
return result;}
public boolean equals(Object other){
if(((Node)other).name.equals(this.name))
return true;
return false;}
}
public static void main(String[] args)
{
Node n = new Node("Lin","Human");
Node k = new Node("Lin","alien");
public static Set<Node> set = new LinkedHashSet<Node>();
set.add(n);
set.add(k); //會視為相同物件,不加入set
想請問有辦法在判斷是相同物件時執行我想自訂的方法嗎?
比如說:當set.add(k)時發現set裡面有相同物件時執行System.out.print("已有相同物");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.165.168.39
推
09/15 02:04, , 1F
09/15 02:04, 1F
推
09/15 02:06, , 2F
09/15 02:06, 2F
→
09/15 02:08, , 3F
09/15 02:08, 3F
推
09/15 13:30, , 4F
09/15 13:30, 4F
→
09/15 13:31, , 5F
09/15 13:31, 5F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):