首页 \ 问答 \ Hashtable返回null但存在对象键(Hashtable returning null but object key is present)

Hashtable返回null但存在对象键(Hashtable returning null but object key is present)

编辑:FML! 我对哈希码的实现有一个小写的c。 -.-

我一直在努力学习TDD并一直关注Kent Beck的“By Example”一书; 这很好!

但是,我似乎无法进步,因为当我访问哈希表时,值返回null。 我运行了一个调试会话,带有值的对象显然是结果为null。

构建和访问的代码是:

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

来自和来自“GBP”和“USD”。 也通过调试验证。

调用上面的测试用例:

@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);
}

银行中的reduce方法调用方法速率:

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

问题出在哪里:

    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();
}

第一行应对美元 - >美元转换等。

Pair对象是2个用作键的字符串。

我没有使用过很多表但是我看不出问题是什么,我确定这些值在哈希表中但是'rate'总是返回一个空值。

我无法看到树木。 :)有人能指出我正确的方向吗?


EDIT: FML! MY implementation of hashcode had a lowercase c. -.-

I've been trying to learn TDD and have been following the 'By Example' book by Kent Beck; it's very good!

However, I can't seem progress because a value is returning null when I access a hashtable. I've run a debug session and the object with the value is clearly there yet the result is 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". 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:

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.

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.

I can't see the wood for the trees. :) Could someone point me in the right direction please?


原文:https://stackoverflow.com/questions/29922190
更新时间:2023-01-07 06:01

最满意答案

关于接受 Iterable<T>这显然是件好事。 这意味着你几乎可以传递任何数据结构 - 它只需要实现非常简单的Iterable<T>接口。 这使代码更易于重用。

为了返回Iterable<T>其中一个优点是方法的实现可以改变 - 可能使用更有效的数据结构,或者在请求时懒洋洋地生成结果(例如,从磁盘流式传输它们)。 在不破坏客户端的情况下更改实现很容易,因为它们仅依赖于Iterable<T>接口。 如果您公开了List<T>那么您无法确定您的客户端是否会按顺序访问数据。

你是对的,如果你需要不止一次访问结果,那么首先将数据复制到不同的结构是有意义的。 但只有您知道哪些特定集合对您的特定情况最有用。 有时它可能是一个ArrayList 。 其他时候您可能更喜欢将项目存储在HashMap 。 因此,您通常必须将数据复制到新的数据结构中。


Regarding accepting an Iterable<T> this is clearly a good thing. It means you can pass it almost any data structure - it only needs to implement the very simple Iterable<T> interface. This makes the code easier to reuse.

For returning an Iterable<T> one of the advantages is that the implementation of the method can change - perhaps using a more efficient data structure, or by generating the results lazily as they are requested (e.g. streaming them from disk). It is easy to change the implementation without breaking your clients, because they only depend on the Iterable<T> interface. If you had exposed a List<T> then you can't be sure that your clients will access the data in sequential order.

You are right that if you need to access the results more than once then it would make sense to first copy the data into a different structure. But only you know what specific collection is most useful for your specific situation. Sometimes it could be an ArrayList. Other times you might prefer to store the items in a HashMap. So you would often have to copy the data into a new data structure anyway.

相关问答

更多
  • JavaFX和JSF是两个非常不同的动物。 我认为JavaFX是Oracle的Adobe Flex(或Flash?)版本。 它运行在JVM上,这意味着它是浏览器的较重客户端。 JSF使用标准的HTML控件和交互。 像JavaFX或Flex一样运行在虚拟机上意味着您不必像使用标准HTML / Javascript那样担心浏览器的功能,但这也意味着您受虚拟机实施者的支配以获得平台浏览器XYZ,并在他们到达时修复平台和安全漏洞。 另一个考虑是某些企业环境可能不喜欢在浏览器中允许像flash / java这样的插件 ...
  • DFS是DFC之上的抽象层。 SourceRebels部分正确,除了EMC现在将DFS视为外部应用程序(API)集成的主要模型的细节。 您不再需要使用编译语言(Java或.Net),因为您可以通过SOAP Web服务调用完成所有操作。 DFC仍然可用于低级别交互,但每次Documentum发布时都会向DFS添加更多服务。 对象模型之一是关键区别。 在DFS中,您可以创建一批操作以发送到服务器以执行(例如,创建10个对象)。 在DFS中还有一些复杂的操作需要更多的代码来完成使用DFC。 DFS还允许您将代码部 ...
  • 在Scala 2.8中,这变得容易得多,有两种方法可以实现。 一种是明确的(虽然它使用含义): import scala.collection.JavaConverters._ val myJavaIterable = someExpr() val myScalaIterable = myJavaIterable.asScala 编辑:自从我写了这个,Scala社区达成了一个广泛的共识,即JavaConverters是好的, JavaConversions是坏的,因为潜在的幽灵行动在一个距离。 所以不 ...
  • 一般 几乎所有已知的HTML解析器都实现了W3C DOM API (JAXP API的一部分,用于XML处理的Java API),并为您提供了一个可以直接使用JAXP API的org.w3c.dom.Document 。 主要的区别通常在解析器的特征中找到。 大多数解析器在一定程度上宽松和宽松的非格式HTML(“tagsoup”),如JTidy , NekoHTML , TagSoup和HtmlCleaner 。 您通常使用这种HTML解析器来“整理”HTML源代码(例如
    通过XML有效的
    ...
  • 从马的嘴里拿出来怎么样? 在github上redex 以防链路在未来中断: 这与ProGuard相比如何? ReDex在概念上类似于ProGuard,因为它们都优化了字节码。 然而,ReDex优化.dex字节码,而ProGuard在将.class字节码降至.dex之前优化.class字节码。 在.dex上运行有时是一个优点:您可以考虑作为内联候选人的方法所使用的虚拟寄存器的数量,并且您可以控制dex文件中的类的布局。 但ProGuard具有很多ReDex不具备的功能(例如,ReDex不会删除未使用的方法参数 ...
  • 关于接受 Iterable这显然是件好事。 这意味着你几乎可以传递任何数据结构 - 它只需要实现非常简单的Iterable接口。 这使代码更易于重用。 为了返回Iterable其中一个优点是方法的实现可以改变 - 可能使用更有效的数据结构,或者在请求时懒洋洋地生成结果(例如,从磁盘流式传输它们)。 在不破坏客户端的情况下更改实现很容易,因为它们仅依赖于Iterable接口。 如果您公开了List那么您无法确定您的客户端是否会按顺序访问数据。 你是对的,如果你需要不止一次访问结果 ...
  • 我假设需要发现需要大量时间的代码,并且可以优化以节省时间。 这与定时例程不同。 我对静态分析仪持怀疑态度,因为一切都取决于输入数据组合。 动态检测尝试测量函数的属性,例如:自身时间和总时间,绝对值,平均值和百分比。 还调用计数,以及每个例程在调用图中的作用。 动态仪器(la gprof )几十年来一直是事实上的标准,但它远不是最后一个词。 首先,重要的是要意识到它给你的大部分统计数据都缺少原始需求。 这些天(恕我直言)你需要一个采样分析器来采样调用堆栈,而不仅仅是程序计数器。 它应该在挂钟时间上进行采样,而 ...
  • 软件包旨在帮助您找到所需的东西。 如果他们比它更容易混淆,那么你做的事情就不对。 如果包装结构不直观,找到课程实际上比平面结构更难。 据我所知,有两个组织课程的基本学校: 首先由模块组织。 这里你的更高级别的软件包是系统的不同模块,你可以按功能进一步分割它。 按功能组织。 在这里,您首先按功能进行组织(例如,一个包中的所有控制器类,另一个包中的所有数据容器等),并可以通过模块进行细分。 这两种系统都有优点和缺点,我觉得它们很平等,尽管我更喜欢模块方法。 真正重要的是要遵循一个系统,不要混淆另一个系统。 不要 ...
  • 您说明这些接口的用途是正确的。 实际上, Iterable声明可以通过提供特定于这些对象的Iterator来迭代实现它的类的对象。 拥有它们是必要的,因为对象应该如何迭代取决于它的内部实现,因此Iterator特定于给定的“集合”类。 话虽如此,值得注意的是,虽然这些接口正式是Java Collections框架的一部分,但它们可以应用于其他情况。 例如,给定一个用于读取CSV文件的虚构API,可以声明一个CsvFile类来实现Iterable>并使用专用的Iterator
  • 因此,编写一个方法cons(Iterable, T) ,使得对concat调用稍微复杂一些。 So, write a method cons(Iterable, T) that makes the slightly-more complicated call to concat.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)