自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

大數(shù)據(jù)之什么是Hash表

大數(shù)據(jù)
大數(shù)據(jù)之什么是Hash表,Hash,一般翻譯做“散列”,也有直接音譯為“哈?!钡?,它是基于快速存取的角度設(shè)計(jì)的,也是一種典型的“空間換時(shí)間”的做法。顧名思義,該數(shù)據(jù)結(jié)構(gòu)可以理解為一個(gè)線性表,但是其中的元素不是緊密排列的,而是可能存在空隙。

大數(shù)據(jù)之什么是Hash表,Hash,一般翻譯做“散列”,也有直接音譯為“哈希”的,它是基于快速存取的角度設(shè)計(jì)的,也是一種典型的“空間換時(shí)間”的做法。顧名思義,該數(shù)據(jù)結(jié)構(gòu)可以理解為一個(gè)線性表,但是其中的元素不是緊密排列的,而是可能存在空隙。

[[278251]]

1.散列表(Hash table,也叫哈希表),是根據(jù)關(guān)鍵碼值(Key value)而直接進(jìn)行訪問的數(shù)據(jù)結(jié)構(gòu)。

也就是說,它通過把關(guān)鍵碼值映射到表中一個(gè)位置來訪問記錄,以加快查找的速度。這個(gè)映射函數(shù)叫做散列函數(shù),存放記錄的數(shù)組叫做散列表。比如我們存儲(chǔ)70個(gè)元素,但我們可能為這70個(gè)元素申請(qǐng)了100個(gè)元素的空間。70/100=0.7,這個(gè)數(shù)字稱為負(fù)載(加載)因子。我們之所以這樣做,也 是為了“快速存取”的目的。我們基于一種結(jié)果盡可能隨機(jī)平均分布的固定函數(shù)H為每個(gè)元素安排存儲(chǔ)位置,以達(dá)到快速存取。但是由于此隨機(jī)性,也必然導(dǎo)致一個(gè)問題就是沖突。所謂沖突,即兩個(gè)元素通過散列函數(shù)H得到的地址相同,那么這兩個(gè)元素稱為“同義詞”。這類似于70個(gè)人去一個(gè)有100個(gè)椅子的飯店吃飯。散列函數(shù)的計(jì)算結(jié)果是一個(gè)存儲(chǔ)單位地址,每個(gè)存儲(chǔ)單位稱為“桶”。設(shè)一個(gè)散列表有m個(gè)桶,則散列函數(shù)的值域應(yīng)為[0,m-1]。

這些元素是按照什么樣的規(guī)則存儲(chǔ)到數(shù)組中呢。一般情況是通過hash(key)%len獲得,也就是元素的key的哈希值對(duì)數(shù)組長(zhǎng)度取模得到。比如上述哈希表中,12%16=12,28%16=12,108%16=12,140%16=12。所以12、28、108以及140都存儲(chǔ)在數(shù)組下標(biāo)為12的位置

2.hash表擴(kuò)容的理解

可是當(dāng)哈希表接近裝滿時(shí),因?yàn)閿?shù)組的擴(kuò)容問題,性能較低(轉(zhuǎn)移到更大的哈希表中).

Java默認(rèn)的散列單元大小全部都是2的冪,初始值為16(2的4次冪)。假如16條鏈表中的75%鏈接有數(shù)據(jù)的時(shí)候,則認(rèn)為加載因子達(dá)到默認(rèn)的0.75。HahSet開始重新散列,也就是將原來的散列結(jié)構(gòu)全部拋棄,重新開辟一個(gè)散列單元大小為32(2的5次冪)的散列結(jié)果,并重新計(jì)算各個(gè)數(shù)據(jù)的存儲(chǔ)位置。以此類推下去.....

負(fù)載(加載)因子:0.75.-->hash表提供的空間是16 也就是說當(dāng)?shù)竭_(dá)12的時(shí)候就擴(kuò)容

3.排重機(jī)制的實(shí)現(xiàn)

假如我們有一個(gè)數(shù)據(jù)(散列碼76268),而此時(shí)的HashSet有128個(gè)散列單元,那么這個(gè)數(shù)據(jù)將有可能插入到數(shù)組的第108個(gè)鏈表中(76268%128=108)。但這只是有可能,如果在第108號(hào)鏈表中發(fā)現(xiàn)有一個(gè)老數(shù)據(jù)與新數(shù)據(jù)equals()=true的話,這個(gè)新數(shù)據(jù)將被視為已經(jīng)加入,而不再重復(fù)丟入鏈表。

4.優(yōu)點(diǎn)

哈希表的插入和查找是很優(yōu)秀的.

對(duì)于查找:直接根據(jù)數(shù)據(jù)的散列碼和散列表的數(shù)組大小計(jì)算除余后,就得到了所在數(shù)組的位置,然后再查找鏈表中是否有這個(gè)數(shù)據(jù)即可。因?yàn)閿?shù)組本身查找速度快,所以查找的效率高低體現(xiàn)在鏈表中,但是真實(shí)情況下在一條鏈表中的數(shù)據(jù)又很少,有的甚至沒有,所以幾乎沒有什么迭代的代價(jià)。所以散列表的查找效率建立在散列單元所指向的鏈表中數(shù)據(jù)的多少上.

對(duì)于插入:數(shù)組的插入速度慢,而鏈表的插入速度快.當(dāng)我們使用哈希表時(shí),不需要更改數(shù)組的結(jié)構(gòu),只需要在找到對(duì)應(yīng)的數(shù)組下標(biāo)后,進(jìn)入對(duì)應(yīng)的鏈表,操作鏈表即可.所以hash表的整體插入速度也很快.

5.模擬實(shí)現(xiàn)代碼

Node類

 

  1. public class Node { 
  2. // key、value模擬鍵值對(duì)的數(shù)據(jù) 
  3.     public Integer key
  4.     public String value; 
  5.     // 下一節(jié)點(diǎn)的引用 
  6.     public Node next
  7.     public Node() { 
  8.     } 
  9.     public Node(int key, String value) { 
  10.         this.key = key
  11.         this.value = value; 
  12.     } 
  13.   

MyLinkedList類

 

  1.  public class MyLinkedList { 
  2.     // 根節(jié)點(diǎn) 
  3.     private Node root; 
  4.   
  5.     public MyLinkedList() { 
  6.         root = new Node(); 
  7.     } 
  8.     /** 
  9.      * 添加數(shù)據(jù),key值必須唯一,如果重復(fù)值將被覆蓋 
  10.      * @param key 
  11.      */ 
  12.     public void add(int key, String value) { 
  13.   
  14.         Node newNode = new Node(key, value); 
  15.         Node current = root; 
  16.         while (current.next != null) { 
  17.             if(current.next.key == key) { 
  18.                 current.next.value = value; 
  19.                 return
  20.             } 
  21.             current = current.next
  22.         } 
  23.         current.next = newNode; 
  24.     } 
  25.   
  26.     /** 
  27.      * 刪除數(shù)據(jù) 
  28.      * @param key 
  29.      * @return 
  30.      */ 
  31.     public boolean delete(int key) { 
  32.   
  33.         Node current = root; 
  34.         while (current.next != null) { 
  35.             if(current.next.key == key) { 
  36.                 current.next = current.next.next
  37.                 return true
  38.             } 
  39.             current = current.next
  40.         } 
  41.         return false
  42.     } 
  43.   
  44.     /** 
  45.      * 根據(jù)key獲取value 
  46.      * @param key 
  47.      * @return 
  48.      */ 
  49.     public String get(int key) { 
  50.   
  51.         Node current = root; 
  52.         while (current.next != null) { 
  53.             if(current.next.key == key) { 
  54.                 return current.next.value; 
  55.             } 
  56.             current = current.next
  57.         } 
  58.         return null
  59.     } 
  60.   
  61.     /** 
  62.      * 遍歷鏈表,列出所有數(shù)據(jù) 
  63.      * @return 
  64.      */ 
  65.     public String list() { 
  66.   
  67.         String str = ""
  68.         Node current = root.next
  69.         while (current != null) { 
  70.             str += "(" + current.key + "," + current.value + "),"
  71.             current = current.next
  72.         } 
  73.         return str; 
  74.     } 
  75.   
  76.     @Override 
  77.     public String toString() { 
  78.         return list(); 
  79.     } 

MyHashMap類

 

  1. // 哈希表 
  2. public class MyHashMap { 
  3.   
  4.     // 鏈表數(shù)組,數(shù)組的每一項(xiàng)都是一個(gè)鏈表 
  5.     private MyLinkedList[] arr; 
  6.     // 數(shù)組的大小 
  7.     private int maxSize; 
  8.   
  9.     /** 
  10.      * 空參構(gòu)造,默認(rèn)數(shù)組大小為10 
  11.      */ 
  12.     public MyHashMap() { 
  13.         maxSize = 10; 
  14.         arr = new MyLinkedList[maxSize]; 
  15.     } 
  16.   
  17.     /** 
  18.      * 帶參構(gòu)造,數(shù)組大小自定義 
  19.      * @param maxSize 
  20.      */ 
  21.     public MyHashMap(int maxSize) { 
  22.         this.maxSize = maxSize; 
  23.         arr = new MyLinkedList[maxSize]; 
  24.     } 
  25.   
  26.     /** 
  27.      * 添加數(shù)據(jù),key值必須唯一 
  28.      * @param key 
  29.      * @param value 
  30.      */ 
  31.     public void put(int key, String value) { 
  32.   
  33.         int index = getHashIndex(key); 
  34.         if(arr[index] == null) { 
  35.             arr[index] = new MyLinkedList(); 
  36.         } 
  37.         arr[index].add(key, value); 
  38.     } 
  39.   
  40.     /** 
  41.      * 刪除數(shù)據(jù) 
  42.      * @param key 
  43.      * @return 
  44.      */ 
  45.     public boolean delete(int key) { 
  46.   
  47.         int index = getHashIndex(key); 
  48.         if(arr[index] != null) { 
  49.             return arr[index].delete(key); 
  50.         } 
  51.         return false
  52.     } 
  53.   
  54.     /** 
  55.      * 根據(jù)key獲取value 
  56.      * @param key 
  57.      * @return 
  58.      */ 
  59.     public String get(int key) { 
  60.   
  61.         int index = getHashIndex(key); 
  62.         if(arr[index] != null) { 
  63.             return arr[index].get(key); 
  64.         } 
  65.         return null
  66.     } 
  67.   
  68.     /** 
  69.      * 獲取數(shù)組下標(biāo) 
  70.      * @param key 
  71.      * @return 
  72.      */ 
  73.     private int getHashIndex(Integer key) { 
  74.         return key.hashCode() % maxSize; 
  75.     } 
  76.   
  77.     /** 
  78.      * 遍歷數(shù)組中所有鏈表的數(shù)據(jù) 
  79.      * @return 
  80.      */ 
  81.     public String list() { 
  82.   
  83.         String str = "[ "
  84.         for (int i = 0; i < maxSize; i++) { 
  85.             if(arr[i] != null) { 
  86.                 str += arr[i].toString(); 
  87.             } 
  88.         } 
  89.         str = str.substring(0, str.length()-1); 
  90.         str += " ]"
  91.         return str; 
  92.     } 
  93.   
  94.     @Override 
  95.     public String toString() { 
  96.         return list(); 
  97.     } 

測(cè)試類

 

  1. public class Test { 
  2.   
  3.     public static void main(String[] args) { 
  4.   
  5.         MyHashMap map = new MyHashMap(20); 
  6.   
  7.         map.put(5, "aaa"); 
  8.         map.put(8, "bbb"); 
  9.         map.put(3, "ccc"); 
  10.         map.put(8, "bbb"); 
  11.         map.put(2, "ddd"); 
  12.         map.put(9, "eee"); 
  13.   
  14.         System.out.println(map); 
  15.         System.out.println(map.get(3)); 
  16.         System.out.println(map.delete(2)); 
  17.         System.out.println(map); 
  18.     } 

 

責(zé)任編輯:未麗燕 來源: 阿里云棲社區(qū)
相關(guān)推薦

2019-03-22 13:53:07

大數(shù)據(jù)架構(gòu)數(shù)據(jù)源數(shù)據(jù)質(zhì)量

2017-12-19 21:29:58

物聯(lián)網(wǎng)區(qū)塊鏈大數(shù)據(jù)

2015-01-27 14:36:18

2016-12-12 16:31:46

大數(shù)據(jù)決策

2018-06-25 11:20:18

LinuxPython大數(shù)據(jù)

2013-11-12 10:50:02

大數(shù)據(jù)時(shí)代大數(shù)據(jù)

2024-11-07 08:47:53

2021-09-29 16:39:23

大數(shù)據(jù)存儲(chǔ)

2021-04-15 09:37:05

大數(shù)據(jù)Maven

2020-12-02 10:32:10

大數(shù)據(jù)存儲(chǔ)

2022-02-27 15:28:53

大數(shù)據(jù)挑戰(zhàn)戰(zhàn)略

2019-07-10 13:02:12

大數(shù)據(jù)智慧城市數(shù)據(jù)安全

2021-04-29 10:08:10

數(shù)據(jù)結(jié)構(gòu)哈希表

2015-03-04 11:01:36

大數(shù)據(jù)數(shù)據(jù)分析分析

2017-04-06 09:35:10

大數(shù)據(jù)SparkSQLSpark

2015-07-29 16:19:54

大數(shù)據(jù)時(shí)代分析

2018-11-05 15:15:38

大數(shù)據(jù)流式數(shù)據(jù)互聯(lián)網(wǎng)

2016-08-16 00:52:19

大數(shù)據(jù)互聯(lián)網(wǎng)

2015-04-23 16:06:05

大數(shù)據(jù)

2019-10-31 15:49:10

大數(shù)據(jù)專家共生關(guān)系
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)