这是Pair.
java
import java.lang.*;
import java.util.*;
public class Pair implements Comparable< Pair > {
protected final TYPEA Key_;
protected final TYPEB Value_;
public Pair(TYPEA key, TYPEB value) {
Key_ = key;
Value_ = value;
}
public TYPEA getKey() {
return Key_;
}
public TYPEB getValue() {
return Value_;
}
public String toString() {
System.out.println("in toString()");
StringBuffer buff = new StringBuffer();
buff.append("Key: ");
buff.append(Key_);
buff.append("\tValue: ");
buff.append(Value_);
return(buff.toString() );
}
public int compareTo( Pair p1 ) {
System.out.println("in compareTo()");
if ( null != p1 ) {
if ( p1.equals(this) ) {
return 0;
} else if ( p1.hashCode() > this.hashCode() ) {
return 1;
} else if ( p1.hashCode() < this.hashCode() ) {
return -1;
}
}
return(-1);
}
public boolean equals( Pair p1 ) {
System.out.println("in equals()");
if ( null != p1 ) {
if ( p1.Key_.equals( this.Key_ ) && p1.Value_.equals( this.Value_ ) ) {
return(true);
}
}
return(false);
}
public int hashCode() {
int hashCode = Key_.hashCode() + (31 * Value_.hashCode());
System.out.println("in hashCode() [" + Integer.toString(hashCode) + "]");
return(hashCode);
}
}
这是测试用例:
import java.lang.*;
import java.util.*;
import junit.framework.*;
public class PairTest extends TestCase {
public void testPair() {
String key = new String("key");
String value = new String("asdf");
Pair pair = new Pair( key, value );
assertTrue( pair.getKey().equals( key ) );
assertTrue( pair.getValue().equals( value ) );
assertTrue( pair.equals( new Pair(key, value)) );
}
public void testPairCollection() {
HashMap< Pair, String> hm1 = new HashMap, String>();
Pair p1 = new Pair("Test1", "Value1");
hm1.put(p1, "ONE");
Pair p2 = new Pair("Test1", "Value2");
hm1.put(p2, "TWO");
Pair p3 = new Pair("Test2", "Value1");
hm1.put(p3, "THREE");
Pair p4 = new Pair("Test2", "Value2");
hm1.put(p4, "FOUR");
Pair p5 = new Pair("Test3", "Value1");
hm1.put(p5, "FIVE");
Pair p6 = new Pair("Test3", "Value2");
hm1.put(p6, "SIX");
Pair p7 = new Pair("Test3", "Value3");
hm1.put(p7, "SEVEN");
assertTrue( hm1.size() == 7 );
Pair pSrch = new Pair("Test3", "Value3");
assertTrue( p7.equals(pSrch) );
assertTrue( pSrch.equals(p7) );
assertTrue( p7.hashCode() == pSrch.hashCode() );
assertTrue( 0 == p7.compareTo( pSrch ) );
assertTrue( 0 == pSrch.compareTo(p7) );
System.out.println("starting containsKey search");
assertTrue( hm1.containsKey( p7 ) );
System.out.println("starting containsKey search2");
assertTrue( hm1.containsKey( pSrch ) );
System.out.println("finishing containsKey search");
String result = hm1.get( pSrch );
assertTrue( null != result );
assertTrue( 0 == result.compareTo("SEVEN"));
}
}
这是我的问题,最后一个hm1.containsKey调用应该(我天真地期望)返回存储在其中的值是的 – 我应该得到一个值为“SEVEN”的字符串.这是输出:
Running in equals()
in hashCode() [1976956095]
in hashCode() [1976956126]
in hashCode() [1976956096]
in hashCode() [1976956127]
in hashCode() [1976956097]
in hashCode() [1976956128]
in hashCode() [1976956159]
in equals()
in equals()
in hashCode() [1976956159]
in hashCode() [1976956159]
in compareTo()
in equals()
in compareTo()
in equals()
starting containsKey search
in hashCode() [1976956159]
starting containsKey search2
in hashCode() [1976956159]
Never reaches
String result = hm1.get( pSrch );
那么p7.hashCode()和pSrch.hashCode()是相等的,p7.equals(pSrch)和pSrch.equals(p7),以及hm1.containsValue(p7)== true,我希望hm1.containsValue(pSrch) )也会返回true,但事实并非如此.我错过了什么?