简体   繁体   English

散列表返回null但存在对象密钥

[英]Hashtable returning null but object key is present

EDIT: FML! 编辑:FML! MY implementation of hashcode had a lowercase c. 我的哈希码实现使用小写字母c。 -.- -.-

I've been trying to learn TDD and have been following the 'By Example' book by Kent Beck; 我一直在尝试学习TDD,并且一直在关注Kent Beck的“通过示例”一书; it's very good! 这很好!

However, I can't seem progress because a value is returning null when I access a hashtable. 但是,我似乎无法取得进展,因为访问哈希表时某个值返回null。 I've run a debug session and the object with the value is clearly there yet the result is null. 我已经运行了一个调试会话,并且带有值的对象显然在那里,但结果为null。

The code to build and access is: 构建和访问的代码为:

public void addRate(String from, String to, int rate){
    this.rates.put(new Pair(from, to), new Integer(rate));
}

from and to are "GBP" and "USD". 从到分别是“ GBP”和“ USD”。 Also verified by debug. 还通过调试验证。

Test case calling the above: 上面的测试用例:

@Test
public void testreduceMoneyDifferentCurrency(){
    Bank bank = new Bank();
    bank.addRate("GBP", "USD", 2);
    Money result = bank.reduce(Money.gbpound(2), "USD");
    assertEquals(Money.dollar(1), result);
}

The reduce method in bank calls the method rate: 银行中的reduce方法调用方法率:

public Money reduce(Bank bank, String to){
    int rate = bank.rate(this.currency, to);
    return new Money(this.amount / rate, to);
}

Which is where the issue is: 问题出在哪里:

    public int rate(String from, String to){
    if (from.equals(to)) return 1;
    Integer rate = (Integer) this.rates.get(new Pair(from, to));
    return rate.intValue();
}

The first line copes with USD -> USD conversions etc. 第一行处理美元->美元转换等。

The Pair object is 2 strings built to be used as a key. Pair对象是为用作键而构建的2个字符串。

I've not used has tables a great deal but I can't see what the issue is, I know for certain that the values are in the hashtable but 'rate' is always returning a null value. 我没有使用过很多表,但是我看不出问题是什么,我肯定知道值在哈希表中,但是“ rate”总是返回空值。

I can't see the wood for the trees. 我看不见树木的树木。 :) Could someone point me in the right direction please? :)有人可以指出我正确的方向吗?

I think the problem is in the Pair method. 我认为问题出在Pair方法中。 When you do this: 执行此操作时:

this.rates.get(new Pair(from, to));

you are creating a new instance of Pair , which is not the same as the one you've put into the map in the addRate method. 您正在创建Pair的新实例,该实例与您在addRate方法中放入地图中的addRate

If you want the code to work correctly, you either have to use the same instance of Pair class or correctly implement equals and hashCode method on Pair class. 如果希望代码正常工作,则必须使用Pair类的相同实例,或者在Pair类上正确实现equalshashCode方法。

Here's a bit deeper insight into the inner working on HashMap and what you have to do to make it work: https://stackoverflow.com/a/6493946/2266098 以下是对HashMap的内部工作以及使它工作所需要做的一些更深入的了解: https : //stackoverflow.com/a/6493946/2266098

Java keeps the reference of objects. Java保留对象的引用。 So when you are trying to do this this.rates.get(new Pair(from, to)); 因此,当您尝试执行此操作时, this.rates.get(new Pair(from, to)); you are basically creating a new instance of Pair which does not exists as a key in your HashMap . 您基本上是在创建一个新的Pair实例,该实例在您的HashMap中不作为键存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM