62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
public class MyKey {
private ArrayList<Long> content;
public ArrayList<Long> getContent() {
return content;
}
public void setContent(ArrayList<Long> content) {
this.content = content;
}
public int hashCode(){
String contentStr = "";
for(int i=0; i<content.size(); i++) {
contentStr += content.get(i) + " ";
}
return contentStr.hashCode();
}
public boolean equals(MyKey obj) {
if (obj instanceof MyKey) {
if(this.content.equals(obj.content))
return true;
}
return false;
}
public String toString(){
return content.toString();
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class HashMapTest {
private static HashMap<MyKey,Integer> map = new HashMap<MyKey,Integer>();
public static void main(String[] args){
ArrayList<Long> list1 = new ArrayList<Long>();
ArrayList<Long> list2 = new ArrayList<Long>();
MyKey mykey1 = new MyKey();
MyKey mykey2 = new MyKey();
list1.add(new Long(1));
list1.add(new Long(2));
list2.add(new Long(1));
list2.add(new Long(2));
mykey1.setContent(list1);
mykey2.setContent(list2);
map.put(mykey1, 3);
System.out.println(map);
System.out.println(map.containsKey(mykey2));
}
}