java key value pair_存储在HashMap中的Java泛型对不能正确检索key-> value

这是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,但事实并非如此.我错过了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值