什么是编程中的哈希映射以及何处可以使用它 [英] What is a hash map in programming and where can it be used

查看:103
本文介绍了什么是编程中的哈希映射以及何处可以使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到人们在讨论散列和散列映射以及散列表。我想知道它们是什么以及哪些地方可以最好地使用它们。

首先,你应该读这篇 article

当你使用列表,你是寻找一个通常需要遍历整个列表的特殊项目。当你有大量的列表时,这是非常昂贵的。

散列表可以快得多,在最好的情况下,你只需要一次访问即可获得你正在寻找的物品。

How它工作吗?就像一本字典......当你在字典中寻找单词hashtable时,你并没有从'a'下的第一个单词开始。但是你直接去看字母'h'。然后到'哈','有'等等,直到你找到你的话。您正在字典中使用索引来加速您的搜索。

哈希表的功能基本相同。每个项目都有一个唯一的索引(所谓的 hash )。你使用这个散列进行查找。哈希可以是正常链接列表中的索引。例如,你的散列可以是2130这样的数字,这意味着你应该查看列表中的位置2130。查询正常列表中的已知索引非常容易且快速。

整个方法的问题是所谓的哈希函数,它分配这个索引给每个项目。当你正在寻找一件物品时,你应该能够提前计算出指数。就像在真正的字典中一样,你看到'hashtable'这个词以字母'h'开始,因此你知道大概的位置。

一个好的散列函数提供了散列码,它们均匀地分布在所有可能的哈希码的空间。当然,它会尽量避免碰撞。当两个不同的项目获得相同的散列码时发生冲突。

在C#中,例如每个对象都有一个 GetHashcode() 方法,它为它提供一个散列(不一定是唯一的)。这可以用于在字典中进行查找和排序。



当您开始使用哈希表时,应始终记住,您正确处理了碰撞。它可以很容易地发生在大型哈希表中,两个对象得到相同的哈希(也许你的GetHashcode()的重载是错误的,也许发生了其他事情)。

I have often heard people talking about hashing and hash maps and hash tables. I wanted to know what they are and where you can best use them for.

解决方案

First you shoud maybe read this article.

When you use lists and you are looking for a special item you normally have to iterate over the complete list. This is very expensive when you have large lists.
A hashtable can be a lot faster, under best circumstances you will get the item you are looking for with only one access.
How is it working? Like a dictionary ... when you are looking for the word "hashtable" in a dictionary, you are not starting with the first word under 'a'. But rather you go straight forward to the letter 'h'. Then to 'ha', 'has' and so on, until you found your word. You are using an index within your dictionary to speed up your search.
A hashtable does basically the same. Every item gets an unique index (the so called hash). You use this hash for lookups. The hash may be an index in a normal linked list. For instance your hash could be a number like 2130 which means that you should look at position 2130 in your list. A lookup at a known index within a normal list is very easy and fast.
The problem of the whole approach is the so called hash function which assigns this index to each item. When you are looking for an item you should be able to calculate the index in advance. Just like in a real dictionary, where you see that the word 'hashtable' starts with the letter 'h' and therefore you know the approximate position.
A good hash function provides hashcodes that are evenly distrubuted over the space of all possible hashcodes. And of course it tries to avoid collisions. A collision happens when two different items get the same hashcode.
In C# for instance every object has a GetHashcode() method which provides a hash for it (not necessarily unique). This can be used for lookups and sorting with in your dictionary.

When you start using hashtables you should always keep in mind, that you handle collisions correctly. It can happen quite easily in large hashtables that two objects got the same hash (maybe your overload of GetHashcode() is faulty, maybe something else happened).

这篇关于什么是编程中的哈希映射以及何处可以使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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