Java 字符串作为HashMap中的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1896072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:39:02  来源:igfitidea点击:

String as a key in HashMap

javahashmap

提问by user230621

I had seen, only the String is used as a key in HashMap.Although the put() method takes Object as a parameter.What is the significant of it.If any other object can also used as a Key or not? Please provide the answers.

我看到过,HashMap中只有String作为key,虽然put()方法以Object为参数,但有什么意义呢。是否还有其他对象也可以作为key呢?请提供答案。

回答by Jon Skeet

You haven't shown which platform you're talking about, but I'll assume for the moment that it's Java.

您还没有显示您在谈论哪个平台,但我暂时假设它是 Java。

You can use any type for the key in a HashMap, but assuming you use the full generic version you need to specify that type and then use it consistent. For example, you could use URI as the key type:

您可以对 a 中的键使用任何类型HashMap,但假设您使用完整的通用版本,您需要指定该类型,然后一致地使用它。例如,您可以使用 URI 作为键类型:

HashMap<URI, Integer> hitCountMap = new HashMap<URI, Integer>();

The getmethod takes Object as the key parameter type, but you should of course use the same type of key that you've put into the map. See this questionfor more discussion on that.

get方法将 Object 作为键参数类型,但您当然应该使用与放入映射中的键类型相同的键。有关更多讨论,请参阅此问题

回答by Gregory Pakosz

Any object that provides a meaningful implementation of hashCode()is a perfect key candidate in a map: see Understanding the workings of equals and hashCode in a HashMap.

任何提供有意义实现的对象hashCode()都是映射中的完美关键候选者:请参阅了解 HashMap 中 equals 和 hashCode 的工作原理

Also, as @Jon mentioned, all keys in your map should be of the same type.

另外,正如@Jon 提到的,地图中的所有键都应该是相同的类型。

EDIT: Of course, you need to implement bothequals()and hashcode(). I thought the title of the link to the other question made that clear. But a dumb implementation of hashcode()will just bring you a degenerate HashMapwhich performance is poor.

编辑:当然,你需要实现两个equals()hashcode()。我认为另一个问题的链接的标题清楚地表明了这一点。但是一个愚蠢的实现hashcode()只会给你带来一个HashMap性能很差的退化。

EDIT2: As @Adrian mentioned in his answer, generics will help you constrain the type of keys and values for the map.

EDIT2:正如@Adrian 在他的回答中提到的,泛型将帮助您限制映射的键和值的类型。

References:

参考:

回答by akuhn

A raw HashMapwill indeed accept any object as key. However, it is good style to specify which kind of keys and values you are going to use in a map

rawHashMap确实会接受任何对象作为键。但是,指定要在地图中使用的键和值的类型是一种很好的风格

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

if you do so, the put()method will only accept strings.

如果这样做,该put()方法将只接受字符串。

 

 

NB: if you choose to use one of your own classes as keys, make sure the class either implements both equalsand hashCodeor none of them!

注:如果您选择使用自己的类中的一个作为密钥,确保类要么实现双方equalshashCode或他们没有!

回答by Stephen C

The key type can be any type, including (for some use-cases) Object. The only technical requirement is that equals(Object)and hashcode()are correctly implemented for all classes whose instances might be used as keys. In practice, you also want the semanticsof equals(Object)to be consistent with the intended behavior of the HashMap, across all possible key types / values.

键类型可以是任何类型,包括(对于某些用例)对象。唯一的技术要求是equals(Object)hashcode()正确的,它的实例可以被用作键的所有类实现的。在实践中,你也想的语义equals(Object)是与HashMap中的预期行为一致,在所有可能的密钥类型/值。

However, if you genuinely do need to use Object as the key type, IdentityHashMap may be a better option. For a start, it doesn't use equals(Object)or hashcode(), but uses ==and the key objects' "identity hash" values.

但是,如果您确实需要使用 Object 作为键类型,那么 IdentityHashMap 可能是更好的选择。首先,它不使用equals(Object)or hashcode(),而是使用==和 关键对象的“身份哈希”值。

回答by sateesh

One probable reason why you see "String" used as hash key quite often is that it is an
immutable class.

您经常看到“String”用作散列键的一个可能原因是它是一个
不可变的类。

Immutable objects make great map keys since one need not worry about their values
being modified once they're in a map or set, which would destroy
map or set's invaraints (Item 15 Effective Java, 2nd Edition)

不可变对象是很好的映射键,因为
一旦它们在映射或集合中,就不必担心它们的值会被修改,这会破坏
映射或集合的不变性(Item 15 Effective Java,第 2 版)

回答by Anand

One of the reason why we generally use String as a key in HashMap is that since String is immutable in Java that allows String to cache its hashcode , being immutable String in Java caches its hash code and do not calculate every time we call hashcode method of String, which makes it very fast as HashMap key.

我们一般在HashMap中使用String作为key的原因之一是因为String在Java中是不可变的,允许String缓存它的hashcode,不可变的String在Java中缓存了它的hash码并且不需要每次调用hashcode方法时计算字符串,这使得它作为 HashMap 键非常快。