Java实现HashTable的基本操作
生活随笔
收集整理的這篇文章主要介紹了
Java实现HashTable的基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 哈希表
- 前言
- 實現思路
- 代碼實現
哈希表
前言
哈希表(Hash Table)也叫做散列表,是根據鍵值對(Key Value)而直接訪問的數據結構。它通過將關鍵碼值Key映射到表的一個位置來直接訪問,以此加快查找的速度。這個映射函數叫做散列函數,存放記錄的數值叫做散列表。
實現思路
哈希表底層通過數組和鏈表組成,數組中的每一個值就是鏈表。
HashMap就是用哈希表實現,當我們使用put(key,value)方法時,哈希表中調用key.hashCode()%size,計算得出的值就是數組的下標,因不同key算出的來下標值可能相同,即使相同不同的key也可用使用鏈表連接起來,由此組成如下圖所示的哈希表。
代碼實現
-
節點Node
class Node {/*** 模擬key-value鍵值對*/public Integer key;public String value;/*** 下一節點的引用*/public Node next;public Node(Integer key, String value) {this.key = key;this.value = value;}public Node() {} } -
鏈表LinkedList
class LinkedList {//頭節點 不存放數據private Node head;public LinkedList(){//初始化頭節點this.head = new Node();}/*** 添加數據,key值必須唯一,否則對value值進行覆蓋* @param key 鍵* @param value 值*/public void add(int key,String value){Node node = new Node(key, value);Node temp = head;while (temp.next != null){//對value值進行覆蓋if (temp.next.key == key){temp.next.value = value;return;}//指針后移temp = temp.next;}//沒有重復key值 向鏈表末尾添加節點temp.next = node;}public boolean delete(int key){if (isEmpty()){System.out.println("鏈表為空....");return false;}Node temp = head;while (temp.next != null){//找到對應key值if (temp.next.key == key){//刪除key所對應節點temp.next = temp.next.next;return true;}temp = temp.next;}return false;}public void list(int num){if (isEmpty()){System.out.printf("鏈表[%d]為空,無法展示數據....\n",(num+1));return;}Node temp = head.next;System.out.printf("鏈表[%d] -> ",(num+1));while (temp != null){System.out.printf("{key:%d,value:%s} -> ",temp.key,temp.value);temp = temp.next;}System.out.print("NULL\n");}public String getValue(int key){if (isEmpty()){System.out.println("鏈表為空,無法獲取數據....");return null;}Node temp = head.next;while (temp != null){if (temp.key == key){return temp.value;}temp = temp.next;}return null;}public boolean isEmpty(){return head.next == null;} } -
哈希表HashTable
class HashTable {private LinkedList[] array;private int size;public HashTable(int size){this.size = size;array = new LinkedList[size];//為每個鏈表進行初始化for (int i = 0; i < size; i++){array[i] = new LinkedList();}}/*** 空參時默認size為8*/public HashTable(){this.size = 8;array = new LinkedList[size];//為每個鏈表進行初始化for (int i = 0; i < size; i++){array[i] = new LinkedList();}}public void add(int key,String value){if (value == null || value == ""){System.out.println("Value值不能為空");return;}int index = hashCode(key);array[index].add(key,value);}public void list(){for (int i = 0; i < size; i++) {array[i].list(i);}}public String getValue(int key){int index = hashCode(key);return array[index].getValue(key);}public void delete(int key){int index = hashCode(key);array[index].delete(key);}public int hashCode(Integer key){return key.hashCode() % size;} } -
測試
/*** @Author: 又蠢又笨的懶羊羊程序猿* @CreateTime: 2021年08月15日 09:51:17*/ public class HashTableDemo {public static void main(String[] args) {//創建哈希表HashTable hashTable = new HashTable();//菜單String select = "";Scanner scanner = new Scanner(System.in);while (true){System.out.println("add:添加節點");System.out.println("list:顯示節點");System.out.println("find:查找節點");System.out.println("del:刪除節點");System.out.println("exit:退出");select = scanner.next();switch (select){case "add":System.out.println("輸入Key:");int key = scanner.nextInt();System.out.println("輸入Value:");String value = scanner.next();hashTable.add(key, value);break;case "list":hashTable.list();break;case "find":System.out.println("請輸入要查找的Key:");key = scanner.nextInt();System.out.printf("key-value->{%d:%s}\n",key,hashTable.getValue(key));break;case "del":System.out.println("請輸入要刪除的key:");key = scanner.nextInt();hashTable.delete(key);break;case "exit":scanner.close();System.out.println("退出....");System.exit(0);}}} }//以下是測試結果 add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出輸入選擇:add 輸入Key: 1 輸入Value: Tom add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:add 輸入Key: 1 輸入Value: Smith add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:list 鏈表[1]為空,無法展示數據.... 鏈表[2] -> {key:1,value:Smith} -> NULL //此時第一次添加的節點已經被覆蓋 鏈表[3]為空,無法展示數據.... 鏈表[4]為空,無法展示數據.... 鏈表[5]為空,無法展示數據.... 鏈表[6]為空,無法展示數據.... 鏈表[7]為空,無法展示數據.... 鏈表[8]為空,無法展示數據.... add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:add 輸入Key: 9 輸入Value: TaylorSwift add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:list 鏈表[1]為空,無法展示數據.... 鏈表[2] -> {key:1,value:Smith} -> {key:9,value:TaylorSwift} -> NULL 鏈表[3]為空,無法展示數據.... 鏈表[4]為空,無法展示數據.... 鏈表[5]為空,無法展示數據.... 鏈表[6]為空,無法展示數據.... 鏈表[7]為空,無法展示數據.... 鏈表[8]為空,無法展示數據.... add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:find 請輸入要查找的Key: 9 key-value->{9:TaylorSwift} //通過key查詢value值 add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:del 請輸入要刪除的key: 1 //通過key刪除節點 add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:list 鏈表[1]為空,無法展示數據.... 鏈表[2] -> {key:9,value:TaylorSwift} -> NULL 鏈表[3]為空,無法展示數據.... 鏈表[4]為空,無法展示數據.... 鏈表[5]為空,無法展示數據.... 鏈表[6]為空,無法展示數據.... 鏈表[7]為空,無法展示數據.... 鏈表[8]為空,無法展示數據.... add:添加節點 list:顯示節點 find:查找節點 del:刪除節點 exit:退出 輸入選擇:exit 退出....進程已結束,退出代碼為 0
以上。
如有不足或者錯誤歡迎評論區指正。
總結
以上是生活随笔為你收集整理的Java实现HashTable的基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java语言实现插值查找
- 下一篇: 数据结构:二叉树