应该足够简单,但是却让我发疯!
具有一个Key:Value对分别为Long:Dto的HashMap。
调试时,我可以看到Hashmap中的一个条目以long值作为键-但是,当我尝试使用Key的get()时,我得到的是空值。
针对以下内容:
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());
答案 0 :(得分:3)
您在哈希图中的键是一个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));
这就是错误的原因。