简体   繁体   English

使用对象作为键的HashMap

[英]HashMap using Object as key

I made a post a few days ago about using a HashMap in a simple banking program, but I'm having issues with using Objects as keys. 我几天前发表了一篇关于在一个简单的银行程序中使用HashMap的文章,但是我在使用对象作为键时遇到了问题。

    HashMap <Account,Client> HM = new HashMap<Account, Client>();
    HM.put(new Account(2193,"Uri"), new Client(2193,0,"Uri"));
    HM.get(2193,"Uri");

Account and Client are classes in other parts of the source. 客户和客户是源代码其他部分中的类。 My issue is that the HM.get isn't working as intended, and is giving me an error. 我的问题是HM.get无法正常工作,并给我一个错误。 Is there another way I'm to 'get' the value? 我还有另一种“获取”价值的方法吗? Not sure how to use the key. 不确定如何使用密钥。 Do note, the setup of the HashMap is without error. 请注意,HashMap的设置没有错误。

Furthermore, is there a better way to go about this? 此外,还有更好的方法吗?

This will give you better idea. 这会给你更好的主意。 that why you need to override hashcode and equals method. 这就是为什么您需要覆盖哈希码和equals方法的原因。

Why do I need to override the equals and hashCode methods in Java? 为什么需要重写Java中的equals和hashCode方法?

After overriding hashcode and equals method. 覆盖哈希码和equals方法之后。

you need to use your object while getting data from hashMap. 您需要在从hashMap获取数据时使用对象。

HM.get(new Account(2193,"Uri"));

First of all this code does not compile as you are passing 2 arguments to get() which expects only 1 argument. 首先,此代码无法编译,因为您要将2个参数传递给get() ,而该参数只需要1个参数。

That argument is supposed to be the key you use in the map and has to be of the same type you declared while declaring your map, in your case HashMap <Account,Client> HM means that HM (which btw should be lowercase by convention) holds as keys objects of type Account and objects of type Client as values. 该参数应该是您在map使用的key ,并且必须与声明地图时声明的类型相同,在这种情况下, HashMap <Account,Client> HM表示HM (按惯例,顺便说一下,小写)将Account类型的对象和Client类型的对象作为值作为键。

It would still compile if you did: 如果您这样做,它仍然可以编译:

get(2193)

Since get() takes an Object but it would simply return a null . 由于get()需要一个Object但它只会返回null

You need to do get(new Account(2193,"Uri")) . 您需要执行get(new Account(2193,"Uri"))

Next you do not need to override equals and hashCode in those classes but it is highly recommended (others already pointed to links saying why). 接下来,您无需在这些类中覆盖equalshashCode ,但强烈建议您这样做(其他人已经指出了原因)。 Also as per the doc you should make the keys immutable so they do not change, otherwise you might get strange behavior. 另外,根据文档,您应该使键不变,这样它们就不会更改,否则您可能会得到奇怪的行为。

Note: great care must be exercised if mutable objects are used as map keys. 注意:如果将可变对象用作地图键,则必须格外小心。 The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. 如果在对象是映射中的键的情况下,以影响等值比较的方式更改对象的值,则不会指定映射的行为。 A special case of this prohibition is that it is not permissible for a map to contain itself as a key. 此禁止的一种特殊情况是,不允许地图包含自身作为键。 While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map. 虽然允许映射包含自身作为值,但建议格外小心:在此类映射上不再很好地定义equals和hashCode方法。

For more detailed description of the Map interface follow Oracle's tutorial 有关Map接口的更多详细说明,请遵循Oracle的教程

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

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