应该足够简单,但是却让我发疯!

有一个HashMap,其Key:Value对分别为Long:Dto。

当我调试时,我可以看到Hashmap中的一个条目以long值作为键-但是当我尝试使用Key进行get()时,我得到的返回值为null。

对于以下内容:

Map<Long, Dto> response = getMap();

System.out.println("Contents of HashMap: " + response);
System.out.println("Size of HashMap: " + response.size());
System.out.println("HashMap Keyset: " + response.keySet());
System.out.println("HashMap Contains: " + response.containsKey(19999984L));
System.out.println("HashMap Object: " + response.get(19999984L));


我得到以下输出:

Contents of HashMap: {19999984={productOptionId=19999984, amount={amount=20.99, currency=EUR}, pricingType=MSRP}}
Size of HashMap: 1
HashMap Keyset: [19999984]
HashMap Contains: false
HashMap Object: null


任何建议,将不胜感激...这应该可以正常工作!

也许我缺少明显的东西。

答:看来我的键值是String类型-如下所示:

System.out.println("Contents of HashMap: " + response);
System.out.println("Size of HashMap: " + response.size());
System.out.println("HashMap Keyset: " + response.keySet());
System.out.println("HashMap Contains: " + response.containsKey("19999984"));
System.out.println("HashMap Object: " + response.get("19999984"));
System.out.println("HashMap Class: " + response.getClass());

最佳答案

您在哈希图中的键是一个int类型,并且您的contains方法具有long类型的19999984L。

Contents of HashMap: {19999984={productOptionId=19999984, amount={amount=20.99, currency=EUR}, pricingType=MSRP}}
Size of HashMap: 1
System.out.println("HashMap Contains: " + response.containsKey(19999984L));


这就是错误的原因。

07-24 09:49
查看更多