Java编程内功-数据结构与算法「哈希表」

开发 后端 算法
散列表(Hash Table,也叫哈希表),是根据关键码值(Key Value)而直接进行访问的数据结构。

[[388064]]

 基本介绍

散列表(Hash Table,也叫哈希表),是根据关键码值(Key Value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表


Google上机题

有一个公司,当有新员工来报到时,要求将该员工的信息加入(id,性别,年龄,地址….),当输入该员工的id时,要求查找该员工的所有信息。

要求:不使用数据库,尽量节省内存,速度越快越好。

  1. package com.xie.hashtable; 
  2.  
  3. import java.util.Scanner; 
  4.  
  5. public class HashTableDemo { 
  6.     public static void main(String[] args) { 
  7.         //创建一个HashTab 
  8.         HashTab hashTab = new HashTab(7); 
  9.  
  10.         String key = ""
  11.         Scanner scanner = new Scanner(System.in); 
  12.         while (true) { 
  13.             System.out.println("add:添加雇员"); 
  14.             System.out.println("list:显示雇员"); 
  15.             System.out.println("find:查找雇员"); 
  16.             System.out.println("delete:删除雇员"); 
  17.             System.out.println("exit:退出程序"); 
  18.             key = scanner.next(); 
  19.             switch (key) { 
  20.                 case "add"
  21.                     System.out.println("输入id"); 
  22.                     int id = scanner.nextInt(); 
  23.                     System.out.println("输入name"); 
  24.                     String name = scanner.next(); 
  25.                     Emp emp = new Emp(id, name); 
  26.                     hashTab.add(emp); 
  27.                     break; 
  28.                 case "list"
  29.                     hashTab.list(); 
  30.                     break; 
  31.                 case "find"
  32.                     System.out.println("请输入编号"); 
  33.                     int no = scanner.nextInt(); 
  34.                     hashTab.findEmpById(no); 
  35.                     break; 
  36.                 case "delete"
  37.                     System.out.println("请输入编号"); 
  38.                     int deleteNo = scanner.nextInt(); 
  39.                     hashTab.deleteEmpById(deleteNo); 
  40.                     break; 
  41.                 case "exit"
  42.                     scanner.close(); 
  43.                     System.exit(0); 
  44.                     break; 
  45.                 default
  46.                     break; 
  47.             } 
  48.         } 
  49.     } 
  50.  
  51. //创建哈希表,管理多条链表 
  52. class HashTab { 
  53.     private int size
  54.     private EmpLinkedList[] empLinkedListArray; 
  55.  
  56.     public HashTab(int size) { 
  57.         this.size = size
  58.         empLinkedListArray = new EmpLinkedList[size]; 
  59.         for (int i = 0; i < size; i++) { 
  60.             empLinkedListArray[i] = new EmpLinkedList(); 
  61.         } 
  62.  
  63.     } 
  64.  
  65.     //添加雇员 
  66.     public void add(Emp emp) { 
  67.         //根据员工的id,得到该员工应当添加到哪条链表 
  68.         int empLinkedListNo = hashFun(emp.id); 
  69.         //将emp添加到对应的链表 
  70.         empLinkedListArray[empLinkedListNo].add(emp); 
  71.     } 
  72.  
  73.     //查找员工 
  74.     public Emp findEmpById(int id) { 
  75.         int no = hashFun(id); 
  76.         Emp empById = empLinkedListArray[no].findEmpById(id); 
  77.         if (empById == null) { 
  78.             System.out.println("不存在id=" + id + "元素"); 
  79.             return null
  80.         } else { 
  81.             System.out.println("存在id=" + id + ",name=" + empById.name); 
  82.             return empById; 
  83.         } 
  84.     } 
  85.  
  86.     //删除雇员 
  87.     public void deleteEmpById(int id) { 
  88.         int no = hashFun(id); 
  89.         empLinkedListArray[no].deleteEmp(id); 
  90.     } 
  91.  
  92.     //遍历哈希表 
  93.     public void list() { 
  94.         for (int i = 0; i < size; i++) { 
  95.             empLinkedListArray[i].list(i); 
  96.         } 
  97.     } 
  98.  
  99.     //编写散列函数,使用简单的取模法 
  100.     private int hashFun(int id) { 
  101.         return id % size
  102.     } 
  103.  
  104.  
  105. //表示雇员 
  106. class Emp { 
  107.     public int id; 
  108.     public String name
  109.     public Emp next
  110.  
  111.     public Emp(int id, String name) { 
  112.         this.id = id; 
  113.         this.name = name
  114.     } 
  115.  
  116.     @Override 
  117.     public String toString() { 
  118.         return "Emp{" + 
  119.                 "id=" + id + 
  120.                 ", name='" + name + '\'' + 
  121.                 '}'
  122.     } 
  123.  
  124. //表示链表 
  125. class EmpLinkedList { 
  126.     //头指针 
  127.     private Emp head; 
  128.  
  129.     //添加雇员到链表 
  130.     //说明: 
  131.     //1.当添加雇员时,id时自增的,即id分配总是从小到大,因此我们将该雇员直接加到本链表的最后即可 
  132.     public void add(Emp emp) { 
  133.         //如果是添加第一个雇员 
  134.         if (head == null) { 
  135.             head = emp; 
  136.         } else { 
  137.             Emp curr = head; 
  138.             while (true) { 
  139.                 if (curr.next == null) { 
  140.                     break; 
  141.                 } 
  142.                 curr = curr.next
  143.             } 
  144.             curr.next = emp; 
  145.         } 
  146.     } 
  147.  
  148.     //遍历 
  149.     public void list(int no) { 
  150.         if (head == null) { 
  151.             System.out.println("第" + (no + 1) + "链表为空"); 
  152.             return
  153.         } 
  154.         System.out.print("第" + (no + 1) + "链表信息为:"); 
  155.         Emp curr = head; 
  156.         while (true) { 
  157.             if (curr != null) { 
  158.                 System.out.printf("=> id=%d,name=%s\t", curr.id, curr.name); 
  159.                 curr = curr.next
  160.             } else { 
  161.                 break; 
  162.             } 
  163.         } 
  164.         System.out.println(); 
  165.     } 
  166.  
  167.     //根据id查找雇员 
  168.     public Emp findEmpById(int id) { 
  169.         if (head == null) { 
  170.             System.out.println("链表为空"); 
  171.             return null
  172.         } 
  173.         Emp temp = head; 
  174.         while (temp != null) { 
  175.             if (temp.id == id) { 
  176.                 return temp
  177.             } 
  178.             temp = temp.next
  179.         } 
  180.         return null
  181.     } 
  182.  
  183.     //根据id删除雇员 
  184.     public void deleteEmp(int id) { 
  185.         if (head == null) { 
  186.             System.out.println("没有id=" + id + "的雇员"); 
  187.             return
  188.         } 
  189.         Emp curr = head; 
  190.         boolean flag = false
  191.         while (true) { 
  192.             if (curr == null) { 
  193.                 break; 
  194.             } 
  195.             if (curr.next.id == id) { 
  196.                 flag = true
  197.                 break; 
  198.             } 
  199.             curr = curr.next
  200.         } 
  201.         if (flag) { 
  202.             curr.next = curr.next.next
  203.         } else { 
  204.             System.out.println("没有id=" + id + "的雇员"); 
  205.         } 
  206.     } 
  207.  

 【编辑推荐】

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-05-12 09:07:09

Java数据结构算法

2021-03-09 06:30:32

JAVA数据结构算法

2021-04-13 09:37:41

Java数据结构算法

2021-03-18 08:44:20

Java数据结构算法

2021-03-26 08:40:28

Java数据结构算法

2021-03-12 09:13:47

Java数据结构算法

2021-03-23 08:33:22

Java数据结构算法

2021-03-10 08:42:19

Java数据结构算法

2021-03-08 06:28:57

JAVA数据结构与算法稀疏数组

2021-04-07 09:26:37

Java数据结构算法

2021-04-16 09:40:52

Java数据结构算法

2021-04-15 09:36:44

Java数据结构算法

2021-04-22 10:07:45

Java数据结构算法

2021-03-14 08:27:40

Java数据结构算法

2021-04-23 09:12:09

Java数据结构算法

2021-03-11 08:53:20

Java数据结构算法

2021-05-13 07:34:56

Java数据结构算法

2021-03-24 10:41:04

Java数据结构算法

2021-05-08 08:28:38

Java数据结构算法

2021-04-27 06:21:29

Java数据结构算法
点赞
收藏

51CTO技术栈公众号