八、哈希表简介

1、介绍

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

2、实例

  • 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id、性别、年龄、住址……),当输入该员工的id时,要求查找到该员工的所有信息.
  • 要求:不使用数据库,尽量节省内存,速度越快越好 => 哈希表(散列)
2.1 代码实现
public class HashTableDemo {

	public static void main(String[] args) {
		// 创建哈希表
		HashTable hashTable = new HashTable(7);

		// 写一个简单的菜单
		String key = "";
		Scanner scanner = new Scanner(System.in);
		System.out.println("add:   【添加员工】");
		System.out.println("list:  【显示员工】");
		System.out.println("find:  【查找员工】");
		System.out.println("delete:【删除员工】");
		System.out.println("exit:  【退出系统】");
		int id;
		Employee employee;
		while (true) {
			key = scanner.next();
			switch (key) {
			case "add":
				System.out.println("输入id:");
				id = scanner.nextInt();
				System.out.println("输入名字:");
				String name = scanner.next();
				// 创建雇员
				employee = new Employee(id, name);
				hashTable.add(employee);
				System.out.println("添加成功,请重新输入选择菜单:");
				break;
			case "list":
				hashTable.list();
				break;
			case "find":
				System.out.println("请输入要查找的id:");
				id = scanner.nextInt();
				hashTable.findEmpById(id);
				System.out.println("请重新输入选择菜单:");
				break;
			case "delete":
				System.out.println("请输入要删除的id:");
				id = scanner.nextInt();
				hashTable.deleteEmpById(id);
				System.out.println("删除成功,请重新输入选择菜单:");
				break;
			case "exit":
				scanner.close();
				System.exit(0);
			default:
				System.out.println("你的输入有误!请重写输入:");
				break;
			}
		}

	}
}

// 创建HashTable,管理多条链表
class HashTable {
	private EmployeeLinkedList[] employeeLinkedListArray;
	private int size;// 表示共有多少条链表

	public HashTable(int size) {
		// 初始化employeeLinkedListArray
		employeeLinkedListArray = new EmployeeLinkedList[size];
		this.size = size;
		// 初始化每个链表!!
		for (int i = 0; i < size; i++) {
			employeeLinkedListArray[i] = new EmployeeLinkedList();
		}
	}

	// 添加雇员
	public void add(Employee employee) {
		// 根据员工的id,得到该员工应当加到哪条链表
		int employeeListNO = hash(employee.getId());
		// 将employee添加到对应的链表
		employeeLinkedListArray[employeeListNO].add(employee);
	}

	// 遍历所有的链表
	public void list() {
		for (int i = 0; i < size; i++) {
			employeeLinkedListArray[i].list(i);
		}
	}

	// 根据输入的id,查找雇员
	public void findEmpById(int id) {
		int employeeListNO = hash(id);
		Employee employee = employeeLinkedListArray[employeeListNO].findEmpById(id);
		if (employee != null) {
			System.out.printf("在第%d条链表找到雇员id= %d\n", employeeListNO + 1, id);
		} else {
			System.out.println("在哈希表中,没有找到该雇员");
		}
	}

	// 根据输入的id,查找雇员
	public void deleteEmpById(int id) {
		int employeeListNO = hash(id);
		// 删除对应节点的链表
		employeeLinkedListArray[employeeListNO].delete(id);
	}

	// 编写一个散列函数使用取模法
	public int hash(int id) {
		return id % size;
	}

}

/**
 * 雇员链表EmployeeLinkedList类
 * 
 * @author user
 *
 */
class EmployeeLinkedList {
	/**
	 * 头指针,指向第一个Emp,因此我们这个链表的head是直接指向第一个Emp,默认为null
	 */
	private Employee head;

	// 添加雇员到链表
	// 假定当添加雇员的时候,id是自增长的,直接加入到链表的最后就行
	public void add(Employee employee) {
		// 如果是添加第一个雇员
		if (head == null) {
			head = employee;
			return;
		}
		// 如果不是第一个雇员,则使用一个辅助指针,帮助我们定位到最后
		Employee curEmployee = head;
		while (true) {
			if (curEmployee.getNext() == null) {
				// 说明到链表最后
				break;
			}
			curEmployee = curEmployee.getNext();
		}
		curEmployee.setNext(employee);
	}

	// 遍历链表的雇员信息
	public void list(int no) {
		if (head == null) {
			// 说明链表为空
			System.out.println("第" + (no + 1) + "条链表为空");
			return;
		}
		System.out.print("第" + (no + 1) + "条链表的信息为:");
		Employee curEmployee = head;
		while (true) {

			System.out.printf("=> id=%d  name=%s\t", curEmployee.getId(), curEmployee.getName());
			if (curEmployee.getNext() == null) {
				// 说明到链表最后
				break;
			}
			curEmployee = curEmployee.getNext();
		}
		System.out.println();
	}

	// 根据雇员id查找雇员
	// 如果查找到,就返回Emp 如果查找不到,就返回null
	public Employee findEmpById(int id) {
		if (head == null) {
			System.out.println("链表为空");
			return null;
		}
		Employee curEmployee = head;
		while (true) {
			if (curEmployee.getId() == id) {
				break;
			}
			// 退出
			if (curEmployee.getNext() == null) {
				curEmployee = null;
				break;
			}

			curEmployee = curEmployee.getNext();
		}
		return curEmployee;
	}

	// 删除雇员
	public void delete(int id) {
		if (head == null) {
			System.out.println("链表为空");
			return;
		}
		if (head.getId() == id) {
			head = null;
			return;
		}
		Employee curEmployee = head;
		while (curEmployee.getNext() != null) {
			if (curEmployee.getNext().getId() == id) {
				curEmployee.setNext(curEmployee.getNext().getNext());
			}
			curEmployee = curEmployee.getNext();
		}
	}
}

/**
 * 用户实体Employee类
 * 
 * @author user
 *
 */
class Employee {
	private int id;
	private String name;
	private Employee next;

	public Employee(int id, String name) {
		super();
		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;
	}

	public Employee getNext() {
		return next;
	}

	public void setNext(Employee next) {
		this.next = next;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + "]";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值