import java.util.HashMap; public class KeyTest { private int i; private int j; public KeyTest(int i,int j) { this.i=i; this.j=j; } public int getI() { return i; } public void setI(int i) { this.i = i; } public int getJ() { return j; } public void setJ(int j) { this.j = j; } public boolean equals(Object obj) { if(obj instanceof KeyTest) { KeyTest oo = (KeyTest)obj; return oo.getI()==this.getI() && oo.getJ()==this.getJ(); }else { return false; } } public int hashCode() { return i+j; } public static void main(String[]args) { HashMap h = new HashMap(); KeyTest key1= new KeyTest(1,2); KeyTest key2= new KeyTest(0,3); h.put(key1,""); h.put(key2,"1233"); System.out.println(h.size());
} }
首先是判断hasCode,如果hasCode不相同,就认为是不同,如果是相同,就还判断equals方法 import java.util.HashMap; public class KeyTest { private int i; private int j; public KeyTest(int i,int j) { this.i=i; this.j=j; } public int getI() { return i; } public void setI(int i) { this.i = i; } public int getJ() { return j; } public void setJ(int j) { this.j = j; } public boolean equals(Object obj) { return false; } public int hashCode() { return 1; } public static void main(String[]args) { HashMap h = new HashMap(); KeyTest key1= new KeyTest(1,2); KeyTest key2= new KeyTest(1,2); h.put(key1,""); h.put(key2,"1233"); System.out.println(h.size());
可是,我需要将一组值作为标记,比如我将2个Integer值作为组合key,这个类该如何实现可以实现在map中标识不同的值,就像用Integer作为key值的时候,插入个相同的Integer值,map在get操作的时候可以发现已经存在了,而我用自定义类,修改hashCode,也不能得到已经插入的数据。
这个在c++里面就是超级简单了。
map.put("1", 1);
map.put("1", 2);
System.out.println(map.get("1"));这样只能获得是2那你现在val用来存放什么呢?
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, key, value, i);
return null;
}
我找到解决方法了,在key类中不仅要实现hashCode,还是实现equals,实现了这2个函数就可以作为key值使用了。至于你提的问题,这只是具体应用的问题。
比如Map<Integer,Integer> 我如果做统计,需要每次get 之后将value+1,就可以保证累加的完成。
ps:个人感觉这种写法效率比c肯定是要低多了(每次都要做2遍查询操作)
import java.util.HashMap;
public class KeyTest {
private int i;
private int j;
public KeyTest(int i,int j)
{
this.i=i;
this.j=j;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public boolean equals(Object obj) {
if(obj instanceof KeyTest)
{
KeyTest oo = (KeyTest)obj;
return oo.getI()==this.getI() && oo.getJ()==this.getJ();
}else
{
return false;
}
}
public int hashCode() {
return i+j;
}
public static void main(String[]args)
{
HashMap h = new HashMap();
KeyTest key1= new KeyTest(1,2);
KeyTest key2= new KeyTest(0,3);
h.put(key1,"");
h.put(key2,"1233");
System.out.println(h.size());
}
}
首先是判断hasCode,如果hasCode不相同,就认为是不同,如果是相同,就还判断equals方法
import java.util.HashMap;
public class KeyTest {
private int i;
private int j;
public KeyTest(int i,int j)
{
this.i=i;
this.j=j;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public boolean equals(Object obj) {
return false;
}
public int hashCode() {
return 1;
}
public static void main(String[]args)
{
HashMap h = new HashMap();
KeyTest key1= new KeyTest(1,2);
KeyTest key2= new KeyTest(1,2);
h.put(key1,"");
h.put(key2,"1233");
System.out.println(h.size());
}
}