HashMap java 中的键
我在 Java 中使用 HashMap 时经常遇到如下场景:
我有一个 A 类对象列表 (List
)
A 有字段 int f1、int f2 和其他字段。
我必须从 List 构造一个映射来对 A 的对象执行 O(1) 查找。关键是 f1 和 f2 的组合(都是整数)。
现在,以下哪一项是用于地图的最佳实践
情况 1:一般情况
情况 2:f2 只能取 2 到 3 个不同的值,而 f1 可以取大量值。
Map<Integer, Map<Integer, List<A>>> // construction of map is cumbersome
Map<String, List<A>> //(key : String f1 + "_" + f2)
Map<Integer, List<A>> //(I tend to use this for case 2)
错过在这里澄清一件事。 f1 和 f2 不能唯一标识 A 的对象。更正了地图定义。
I usually come across scenarios while using HashMap in Java as follows :
I've a list of Objects of class A (List<A>
)
A has fields int f1, int f2 and other fields.
I've to construct a map from List to perform O(1) lookup for the Objects of A. The key is combination of f1 and f2 (both being integers).
Now which of the following would be the best practice to use for the map
case 1 : in general
case 2 : f2 can take only 2 to 3 different values, while f1 can take large number of values.
Map<Integer, Map<Integer, List<A>>> // construction of map is cumbersome
Map<String, List<A>> //(key : String f1 + "_" + f2)
Map<Integer, List<A>> //(I tend to use this for case 2)
Missed to clarify one thing here. f1 and f2 don't uniquely identify objects of A. Corrected the map definitions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果这两个字段往往是不可变的(它们一旦设置就不会改变),您可以重写 A 的 equals() 和 hashCode() 方法,并简单地存储a:
如果它们不是不可变的,则无论如何您都不能将它们用作密钥,因为它们可能会发生变化。
If those two fields tend to be immutable (they don't change once set), you can override the
equals()
andhashCode()
methods of A, and simply store a:If they are not immutable, you cannot use them for the key anyway, since they might change.
我认为Map适合情况1,对于情况,我推荐List,这个列表只有2-3个元素,那么你可以将索引映射到特定的字段值。
I think Map is suitable for case 1, and for case, i recommend List, and this list only have 2-3 elements, then you can map an index to the specific field value.
为什么要使用地图?如果您确实不需要键值对,则可以仅使用
HashSet
。查找仍然是 O(1),并且您不必费心从键中获取值。当然,HashSet 可能只是一个具有 null 值的 HashMap,但您不必发明键和值。
Why use a map at all? If you don't really need Key-Value pairs, you can just use a
HashSet<A>
. The lookup is still O(1) and you don't have to bother getting a value from the key.Of course, the HashSet is probably just a HashMap with null values, but you don't have to invent keys and values.
我不喜欢使用字符串作为复合键。一些博主说得很好:字符串适用于文本内容,但不适用于非文本内容。
为什么不创建一个简单的带有两个 int 字段以及适当的 hashCode() 和 equals(Object) 的 IntPair 类> 覆盖?在 IDE 中,这将花费您两秒钟的时间(如果没有 IDE,时间不会太长),并且您将获得更具体、语义上有意义的密钥类型。
I don't like using Strings as composite keys. Some blogger out there put it well: Strings are good for things that are text, and not good for things that aren't text.
Why not just create a simple
IntPair
class with twoint
fields, and appropriatehashCode()
andequals(Object)
overrides? It'll take you two seconds in an IDE (not much longer without one), and you'll have a more specific, semantically meaningful key type.键在 HashMap 中是唯一的...因为在 java 内部键被设置为
java 中的 int static Entry 类
这就是为什么键是唯一的,它不允许重复...
Key is unique in HashMap...because internally in java key is set as
int static Entry class in java
That's why the key is unique it won't allow duplicates...