Reference: Wiki  PrincetonAlgorithm

What is Hash Table

Hash table (hash map) is a data structure used to implement an associative array, a structure that can map keys to values.
A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

hash function: transform the search key into an array index

What is Hash Collisions

Different keys that are assigned by the hash function to the same bucket.

Perfect Hashing

A perfect hash function for a set S is a hash function that maps distinct elements in S to a set of integers, with no collisions.

Perfect hashing allows for constant time lookups in the worst case. This is in contrast to most chaining and open addressing methods, where the time for lookup is low on average, but may be very large, O(n), for some sets of keys.

Load Factor

A critical statistic for a hash table.

Load factor = n / k

where:
n = number of entries
k = number of buckets

  • high load factor: hash table becomes slower, and it may even fail to work (depending on the method used).
  • low load factor: considering the proportion of unused areas in the hash table. This results in wasted memory.

Also, one should examine the variance of number of entries per bucket. For example, two tables both have 1000 entries and 1000 buckets; one has exactly one entry in each bucket, the other has all entries in the same bucket. Clearly the hashing is not working in the second one.

Hash Functions

3 primary requirements in implementing a good hash function for a given data type:

  1. It should be deterministic - equal keys must produce the same hash value.
  2. It should be efficient to compute.
  3. It should uniformly distribute the keys.

Suppose we have an array that can hold M key-value pairs, then we need a function that can transform any given key into an index into that array.

Positive Integers

modular hashing

Choose the array size M to be prime, and, for any positive integer key k, compute the remainder when dividing k by M. (k % M)

Floating-Point Numbers

Simple but defective way:
If the keys are real numbers between 0 and 1, we might just multiply by M and round off to the nearest integer to get an index between 0 and M-1.
This approach is defective because it gives more weight to the most significant bits of the keys; the least significant bits play no role.
Better way (adopted by Java):
Use modular hashing on the binary representation of the key.

String 

Simply treat them as huge integers.

R is prime number, Java uses 31. Calculate each bit of the array.

int hash = 0;
for (int i = 0; i < s.length(); i++)
hash = (R * hash + s.charAt(i)) % M;

Also, we can use MD5 to randomize input keys

int hashFunc(String key) {
return md5(key) % HASH_TABLE_SIZE;
}

APR hash function uses magic number 33. Similar with the first method.

int hashFunc(String key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum = sum * 33 + (int) key.charAt(i);
sum = sum % HASH_TABLE_SIZE;
}
return sum;
}

Compound Keys

We can follow the ways of processing string.

For example, we use a tuple as key (element1, element2, element3)

int hash = (((element1 * R + element2) % M) * R + element3) % M;

By Using hashCode() in Java

private int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % M;

Collision Resolutions

Generally, there are four ways to resolve collision:

  1. Separate Chaining
  2. Open Addressing
  3. Robin Hood Hashing
  4. 2-Choic Hashing

details check here

Hash Map (Hash Table)的更多相关文章

  1. [LeetCode] Repeated DNA Sequences hash map

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. POJ 1840 Eqs 二分+map/hash

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  3. Implement Hash Map Using Primitive Types

    A small coding test that I encountered today. Question Using only primitive types, implement a fixed ...

  4. hdu1381 Crazy Search(hash map)

    题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...

  5. (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅

    字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...

  6. Hash Map 在java中的解释及示例

    目录 HashMap在java中的应用及示例 HashMap的内部结构 HashMap的性能 同步HashMap HashMap的构造函数 HashMap的时间复杂度 HashMap的方法 1. vo ...

  7. TTTTTTTTTTTT 百度之星D map+hash

    Problem D  Accepts: 2806  Submissions: 8458  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  8. Redis的增删改查 c# key value类型和hash map 类型

    using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; us ...

  9. hdu 4622 (hash+“map”)

    题目链接:https://vjudge.net/problem/HDU-4622 题意:给定t组字符串每组m条询问--求问每条询问区间内有多少不同的子串. 题解:把每个询问区间的字符串hash一下存图 ...

随机推荐

  1. java类的加载顺序

    related URL: http://www.cnblogs.com/guoyuqiangf8/archive/2012/10/31/2748909.html Parent Class: packa ...

  2. Ubuntu 12.04 搭建Android开发环境

    Ubuntu 12.04 搭建Android开发环境 2013/7/29 Linux环境下搭建Android开发环境 大部分开发人员可能都在Windows下做开发,可能是感觉在Windows下比较方便 ...

  3. cocos2d-x 2.x 图层特效Effect(转)

    CCSprite* sp = CCSprite::create("Default.png"); sp->setPosition(ccp(, )); addChild(sp); ...

  4. js中setTimeout/setInterval定时器用法示例

    js中setTimeout(定时执行一次)和setInterval(间隔循环执行)用法介绍. setTimeout:在指定的毫秒数后调用指定的代码段或函数:setTimeout示例代码 functio ...

  5. ASP.NET静态页生成方法(模板替换)

    本文实例讲述了ASP.NET静态页生成方法的一种简单方法,就是替换内容法. 适用场景 模板比较固定,页面替换内容较少. 基本原理 此方法中静态页生成用到的就是匹配跟替换了,首先得读取模板页的html内 ...

  6. Excel01-不同的单元格输入同一数据

    第一步:按住Ctrl键,选择不同的单元格 第二步:选择完最后一个单元格后,输入需要的数据“YES”,按Ctrl+Enter键结束. 提示:按Ctrl+; 输入当前日期,再按ctrl+Enter实现全部 ...

  7. SuperSocket快速入门(二):启动程序以及相关的配置

    如何快速启动第一个程序 既然是快速入门,所以,对于太深奥的知识点将不做讲解,会在后续的高级应用章节中,会对SS进行拆解.所有的实例90%都是来自SS的实例,外加本人的注释进行讲解. 一般应用而言,你只 ...

  8. Aspose.Words导出dt到word的问题

    已解决:单挑数据导入到一个word文档,导出文字和图片成功,执行Main();方法导出dt到word 待解决:多条数据(文字,图片的导入到一个word文档里面)从dt导入到word;已发现的错误提示“ ...

  9. MVC 5.0 之奇葩错误-<类型“ASP._Page__ViewStart_cshtml”不从“System.Web.WebPages.StartPage”继承>

    在实际项目中,我们通常添加MVC项目会先添加一个MVC Empty 的项目,然后需要什么在往里面添加. 但是Empty项目里面只有一个路由注册,而且没有_ViewStart.cshtml文件需要自己添 ...

  10. foreach遍历扩展(二)

    一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...