简体   繁体   English

我们可以在其他地图中使用嵌套地图作为键吗?

[英]can we have a nested map as key within other map?

I have just started implementing data structures in Java and was wondering can we have a situation like this.我刚刚开始在 Java 中实现数据结构,想知道我们是否会遇到这样的情况。

Map<HashMap<String,String>,String> map = new HashMap<HashMap<String,String>,String>(); 

And if yes ,, please give a small example.如果是,请举一个小例子。

If you don't found question relevant ,, please mention in comments,如果您没有发现相关问题,请在评论中提及,

You can do this, but you should not do so in most cases.可以这样做,但在大多数情况下不应该这样做。

The key to a map needs to be constant and its equals and hashcode need to be set up to give the correct behavior.映射的键需要保持不变,并且需要设置其等于和哈希码以提供正确的行为。 If you modify a key after adding it into the map then you invalidate the map.如果在将键添加到映射后修改键,则会使映射无效。

A HashMap can be modified so should not be used as a key. HashMap 可以修改,因此不应用作键。

To explain why changing it is a problem you need to know how hashmaps work.要解释为什么更改它是一个问题,您需要了解哈希图的工作原理。 This is very simplified but lets say you have a HashMap H containing two buckets.这非常简化,但假设您有一个包含两个存储桶的HashMap H Lets call them B0 and B1.让我们称它们为 B0 和 B1。

Whenever you add an Object to the HashMap it looks at that objects hashCode .每当您将Object添加到HashMap它都会查看该对象hashCode If the final bit is 0 then it goes in B0, if it is 1 then it goes in B1.如果最后一位为 0,则进入 B0,如果最后一位为 1,则进入 B1。

Now when looking up an object it looks at the hashCode and immediately goes to the right bucket, it then only needs to search the objects in that bucket to find the object it needs.现在,在查找对象时,它会查看hashCode并立即转到正确的存储桶,然后只需搜索该存储桶中的对象即可找到所需的对象。

By using more buckets than just 2 you can reduce the number of items in each bucket by 2, 4, 8 or more times and hence reduce the number of objects you need to check.通过使用比 2 个更多的存储桶,您可以将每个存储桶中的项目数量减少 2、4、8 或更多倍,从而减少您需要检查的对象数量。

However lets say you put an object in the map and it gets added to B0.但是,假设您将一个对象放入地图并将其添加到 B0。 You then change the object and the hashCode changes so the last bit is now 1.然后你改变对象,hashCode 改变,所以最后一位现在是 1。

if you do map.contains(obj) you will get the result false because it will look at the hashCode, jump straight to B1 and only scan that for the object.如果你做map.contains(obj)你会得到结果 false 因为它会查看 hashCode,直接跳转到 B1 并且只扫描对象。 But the object was placed in B0 because that is what the hashCode had at the time it was inserted.但是该对象被放置在 B0 中,因为这是插入时 hashCode 所拥有的。

This is why the hashCode must be constant for any object being used as a key in a HashMap, as otherwise you can "lose" these keys.这就是为什么对于在 HashMap 中用作键的任何对象,hashCode 必须是常量的原因,否则您可能会“丢失”这些键。

HashMap<String, String> map = new HashMap<String, String>();
        HashMap<HashMap<String, String>, String> tMap = new HashMap<HashMap<String, String>, String>();
        map.put("1", "one");
        map.put("2", "two");
        tMap.put(map, "numbers");
        //for getting the value
        Set description = tMap.entrySet();

Use Iterator to iterate values for hashmap values Implement this type of fucntionality based on your needs otherwise it is not recommended.使用 Iterator 迭代 hashmap 值的值根据您的需要实现这种类型的功能,否则不推荐。

You can't use a Map as a key but you can use it as a value.您不能将 Map 用作键,但可以将其用作值。

To explain further, since you are likely to be adding to the HashMap the key will not remain constant and thus will no longer serve it's purpose as a key.进一步解释一下,由于您可能要添加到 HashMap 中,键将不会保持不变,因此将不再用作键的用途。

正如其他答案中提到的, HashMap不应该作为映射键,因为它是可变的,但是,不可变映射(例如 Guava 的ImmutableMap)将起作用。

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

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