什么应该是在一个哈希表中关键的最佳实践 [英] Best practices on what should be key in a hashtable

查看:144
本文介绍了什么应该是在一个哈希表中关键的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的查找结构是一个的HashTable 。它提供了平均的持续访问的(线性在最坏的情况下)。
这取决于散列函数。好吧。
我的问题是下面。假设一个很好的实施了的HashTable 如: 的HashMap 有没有关于地图通过密钥的最佳做法?我的意思是,建议重点必须是不可变的对象,但我不知道是否还有其他建议。
例如,密钥的长度?例如,在一个良好的HashMap(以上述方式),如果我们使用字符串钥匙,不会的瓶颈是在为字符串比较等于(试图找到钥匙)?所以应的按键保持较小?还是有不应该被用作键的对象?例如。一个网​​址?在这种情况下,您如何可以选择一键使用什么?

The best look-up structure is a HashTable. It provides constant access on average (linear in worst case).
This depends on the hash function. Ok.
My question is the following. Assuming a good implementation of a HashTable e.g. HashMap is there a best practice concerning the keys passed in the map?I mean it is recommended that the key must be an immutable object but I was wondering if there are other recommendations.
Example the size of the key? For example in a good hashmap (in the way described above) if we used String as keys, won't the "bottleneck" be in the string comparison for equals (trying to find the key)? So should the keys be kept small? Or are there objects that should not be used as keys? E.g. a URL? In such cases how can you choose what to use as a key?

推荐答案

表现最好的关键的HashMap的可能是一个整数,其中散code()等于()实现为:

The best performing key for an HashMap is probably an Integer, where hashCode() and equals() are implemented as:

public int hashCode() {
    return value;
}

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

说,一个HashMap中的目的是要映射某些对象(值)的一些其他(键)。该散列函数是用于寻址(值)的事实对象是提供快速,恒定时访问。

Said that, the purpose of an HashMap is to map some object (value) to some others (key). The fact that a hash function is used to address the (value) objects is to provide fast, constant-time access.

建议的关键必须是不可变的对象,但我不知道是否还有其他建议。

it is recommended that the key must be an immutable object but I was wondering if there are other recommendations.

的建议是将对象映射到你所需要的:不觉得有什么比较快;但想想什么是最适合你的业务逻辑处理对象来检索。

The recommendation is to Map objects to what you need: don't think what is faster; but think what is the best for your business logic to address the objects to retrieve.

最重要的要求是,重点对象必须是不可变,因为如果你将其存储在地图可能无法在以后检索相关的值后更改密钥对象。

The important requirement is that the key object must be immutable, because if you change the key object after storing it in the Map it may be not possible to retrieve the associated value later.

关键字的在的HashMap 地图。你的目标应该只的地图的。如果你牺牲了测绘任务优化的关键,你击败了地图的目的 - 没有可能实现任何性能提升。

The key word in HashMap is Map. Your object should just map. If you sacrifice the mapping task optimizing the key, you are defeating the purpose of the Map - without probably achieving any performance boost.

我100%同意你的问题中的前两个意见:

I 100% agree with the first two comments in your question:

主要的制约因素是,它必须要立足于查找的东西;)
   - 奥利查尔斯沃思

the major constraint is that it has to be the thing that you want to base the lookup on ;)
– Oli Charlesworth

一般的规则是因为无论你需要查找的键使用。
   - 路易·沃瑟曼

The general rule is to use as the key whatever you need to look up with.
– Louis Wasserman

记住优化的两条规则:

  1. 请不要。
  2. (仅适用于专家)不还。

第三条规则是:配置文件之前优化

这篇关于什么应该是在一个哈希表中关键的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆