简体   繁体   English

使用key获取hashmap的方法,因为另一个hashmap始终返回null值

[英]Get method of an hashmap with key as another hashmap always returns null values

I have two hashmaps. 我有两个哈希图。 Each of those have keys as other hashmaps like the one below. 其中每个都有键作为其他哈希图,如下所示。

Map<Map<String, Object>, Map<String, Object>>

This map gets filled from values of a DB2 database query output. 此映射从DB2数据库查询输出的值填充。 I have noticed that even when I have two such hashmaps and if I try to retrieve the value for a hashmap key, it always returns me null. 我注意到即使我有两个这样的hashmaps,如果我尝试检索hashmap键的值,它总是返回null。 I have checked by adding all the needed variables in watch window. 我通过在监视窗口中添加所有需要的变量来检查。 Everything looks good to me. 一切看起来都很好。

Small snippet of my code. 我的代码的小片段。

mapTransactionNumberTranIds
                .put(inputMessageDbRecord
                        .get(i)
                        .get(prop.getProperty(
                                BloombergConstants.INPUT_BBR_TRANS_NO).toString())
                        .toString(),
                        inputMessageDbRecord
                                .get(i)
                                .get(prop.getProperty(
                                        BloombergConstants.TRAN_ID)
                                        .toString()).toString());

Note: inputMessageDbRecord is of type List<Map<String, Object>> and mapTransactionNumberTranIds is of type Map<Map<String, Object>, Map<String, Object>> 注意:inputMessageDbRecord的类型为List<Map<String, Object>> ,mapTransactionNumberTranIds的类型为Map<Map<String, Object>, Map<String, Object>>

You also have to understand that the key will only work if you have the exact same object instance for adding and retrieving from the map and the key is not allowed to never change, because comparison will check if the key is the same instance using equals() and hashCode(). 您还必须了解,只有当您具有完全相同的对象实例以便从地图添加和检索时才能使用该密钥,并且不允许密钥永远不会更改,因为比较将使用equals检查密钥是否是相同的实例( )和hashCode()。

This article describes issues about using object as key in a hashmap. 本文介绍有关在hashmap中使用object作为键的问题。

Most likely, the problem you are facing is related to those requirements. 最有可能的是,您遇到的问题与这些要求有关。

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

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