Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a hash table?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is a hash table". In the daily operation, I believe that many people have doubts about what is a hash table. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is a hash table"! Next, please follow the editor to study!

Basic introduction

A hash table (Hash Table, also known as a hash table) is a data structure that is accessed directly based on key code values (Key Value). That is, it accesses the record by mapping the key value to a location in the table to speed up the lookup. This mapping function is called a hash function, and the array where records are stored is called a hash table.

The computer questions on Google

There is a company that, when a new employee comes to check in, requires that the employee's information be added (id, gender, age, address … .), when entering the employee's id, it is required to find all the information about the employee.

Requirements: do not use the database, try to save memory, the faster the better.

Package com.xie.hashtable; import java.util.Scanner; public class HashTableDemo {public static void main (String [] args) {/ / create a HashTab HashTab hashTab = new HashTab (7); String key = ""; Scanner scanner = new Scanner (System.in); while (true) {System.out.println ("add: add employees") System.out.println ("list: show employees"); System.out.println ("find: find employees"); System.out.println ("delete: delete employees"); System.out.println ("exit: exit the program"); key = scanner.next () Switch (key) {case "add": System.out.println ("input id"); int id = scanner.nextInt (); System.out.println ("input name"); String name = scanner.next (); Emp emp = new Emp (id, name) HashTab.add (emp); break; case "list": hashTab.list (); break; case "find": System.out.println ("Please enter the number"); int no = scanner.nextInt () HashTab.findEmpById (no); break; case "delete": System.out.println ("Please enter the number"); int deleteNo = scanner.nextInt (); hashTab.deleteEmpById (deleteNo); break Case "exit": scanner.close (); System.exit (0); break; default: break;} / / create hash tables and manage multiple linked lists class HashTab {private int size; private EmpLinkedList [] empLinkedListArray Public HashTab (int size) {this.size = size; empLinkedListArray = new EmpLinkedList [size]; for (int I = 0; I

< size; i++) { empLinkedListArray[i] = new EmpLinkedList(); } } //添加雇员 public void add(Emp emp) { //根据员工的id,得到该员工应当添加到哪条链表 int empLinkedListNo = hashFun(emp.id); //将emp添加到对应的链表 empLinkedListArray[empLinkedListNo].add(emp); } //查找员工 public Emp findEmpById(int id) { int no = hashFun(id); Emp empById = empLinkedListArray[no].findEmpById(id); if (empById == null) { System.out.println("不存在id=" + id + "元素"); return null; } else { System.out.println("存在id=" + id + ",name=" + empById.name); return empById; } } //删除雇员 public void deleteEmpById(int id) { int no = hashFun(id); empLinkedListArray[no].deleteEmp(id); } //遍历哈希表 public void list() { for (int i = 0; i < size; i++) { empLinkedListArray[i].list(i); } } //编写散列函数,使用简单的取模法 private int hashFun(int id) { return id % size; } } //表示雇员 class Emp { public int id; public String name; public Emp next; public Emp(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + '}'; } } //表示链表 class EmpLinkedList { //头指针 private Emp head; //添加雇员到链表 //说明: //1.当添加雇员时,id时自增的,即id分配总是从小到大,因此我们将该雇员直接加到本链表的最后即可 public void add(Emp emp) { //如果是添加第一个雇员 if (head == null) { head = emp; } else { Emp curr = head; while (true) { if (curr.next == null) { break; } curr = curr.next; } curr.next = emp; } } //遍历 public void list(int no) { if (head == null) { System.out.println("第" + (no + 1) + "链表为空"); return; } System.out.print("第" + (no + 1) + "链表信息为:"); Emp curr = head; while (true) { if (curr != null) { System.out.printf("=>

Id=%d,name=%s\ t ", curr.id, curr.name); curr = curr.next;} else {break;}} System.out.println () } / / find employee public Emp findEmpById (int id) {if (head = = null) {System.out.println ("linked list is empty") according to id; return null;} Emp temp = head; while (temp! = null) {if (temp.id = = id) {return temp } temp = temp.next;} return null;} / / Delete employee public void deleteEmp (int id) {if (head = = null) {System.out.println ("employee without id=" + id + ") according to id; return;} Emp curr = head; boolean flag = false While (true) {if (curr = = null) {break;} if (curr.next.id = = id) {flag = true; break;} curr = curr.next } if (flag) {curr.next = curr.next.next;} else {System.out.println ("employees without id=" + id + ");}} at this point, the study on" what is a hash table "is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report