package Hashmap;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap> TT = new HashMap<>();
ArrayList gIds = null;
Test t1=new Test(0, 1, 0, 0, 0);
Test t2=new Test(0, 1, 0, 0, 0);
TT.put(t1, gIds);
if (!TT.containsKey(t2)) {
System.out.println("不含");
} else {
gIds = TT.get(t2);
System.out.println("找到");
}
}
}
class Test{
public int a;
public int b;
public int c;
public int d;
public int e;
public Test(int a,int b,int c,int d,int e){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
this.e=e;
}
}
輸出的結果是不含,請問為什麼,我該怎麼改呢?
需要重寫equals,否則比較的是引用,不是你的屬性。