package day05; import java.sql.SQLOutput; import java.util.Scanner; /** * 哈希表代碼實現 */ public class HashTab { public static void main(String[] args) { System.out.println("-----------功能-----------"); System.out.println("查詢所有員工:findAll"); System.out.println("查詢指定ID員工:findOne"); System.out.println("添加員工:add"); System.out.println("刪除員工:delete"); System.out.println("清空員工:clear"); System.out.println("--------------------------"); while (true) { System.out.print("請輸入需要執行功能:"); String handle = new Scanner(System.in).next(); if (handle.equals("add")) { System.out.print("請輸入用戶ID:"); int id = new Scanner(System.in).nextInt(); System.out.print("請輸入用戶姓名:"); String name = new Scanner(System.in).next(); Emp emp = new Emp(id, name); add(emp); } else if (handle.equals("findAll")) { findAll(); } else if (handle.equals("findOne")) { System.out.print("請輸入要查詢員工ID:"); int id = new Scanner(System.in).nextInt(); findOne(id); } else if (handle.equals("delete")) { System.out.print("請輸入要刪除員工ID:"); int id = new Scanner(System.in).nextInt(); delete(id); } else if (handle.equals("clear")) { System.out.println("正在清理所有員工信息..."); clearEmp(); } else if (handle.equals("exit")) { System.out.println("退出功能..."); break; } } } public static final int SIZE = 7; public static EmployeeList[] employeeLists = new EmployeeList[SIZE]; static { for (int i = 0; i < employeeLists.length; i++) { employeeLists[i] = new EmployeeList(); } } private static void delete(int id) { int hashIndex = hashIndex(id); EmployeeList employeeList = employeeLists[hashIndex]; employeeList.delete(id); } private static void findOne(int id) { int hashIndex = hashIndex(id); EmployeeList employeeList = employeeLists[hashIndex]; employeeList.findOne(id); } public static void findAll() { for (int i = 0; i < employeeLists.length; i++) { System.out.print("第" + (i + 1) + "條鏈表:"); employeeLists[i].findAll(); System.out.println(); } } public static void add(Emp emp) { if (emp == null) { throw new IllegalArgumentException("添加對象不能爲空"); } int hashIndex = hashIndex(emp.getId()); EmployeeList employeeList = employeeLists[hashIndex]; employeeList.add(emp); } private static void clearEmp() { for (int i = 0; i < employeeLists.length; i++) { EmployeeList employeeList = employeeLists[i]; employeeList.claerEmp(); System.out.println("清理所有員工結束..."); } } /** * 對操作對象的Id進行hash(此處取餘) * * @param id * @return */ public static int hashIndex(int id) { return id % SIZE; } } /** * 員工鏈表類 */ class EmployeeList { public Emp head = new Emp(); /** * 添加用戶 * * @param emp */ public void add(Emp emp) { Emp cur = head; while (true) { if (cur.next == null) { cur.next = emp; System.out.println(emp.getName() + "添加成功"); break; } cur = cur.next; } } /** * 打印該鏈表所有用戶 */ public void findAll() { Emp cur = head.next; while (true) { if (cur == null) { System.out.print("null"); break; } System.out.print("[" + cur.getId() + "," + cur.getName() + "]->"); cur = cur.next; } } /** * 根據員工ID查詢 * * @param id */ public void findOne(int id) { Emp cur = head.next; while (true) { if (cur == null) { System.out.println(id + "對應的員工不存在."); break; } if (cur.getId() == id) { System.out.println("[" + cur.getId() + "," + cur.getName() + "]"); break; } else { cur = cur.next; } } } /** * 刪除員工 * * @param id */ public void delete(int id) { Emp cur = head; while (true) { if (cur.next == null) { System.out.println(id + "對應的員工不存在."); break; } if (cur.next.getId() == id) { cur.next = cur.next.next; break; } else { cur = cur.next; } } } public void claerEmp() { if (head.next == null) { return; } else { head.next = null; } } } /** * 員工類 */ class Emp { private int id; private String name; public Emp next; public Emp() { } public Emp(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + '}'; } }