看一个实际需求,Google公司的一个上机题
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,姓名,年龄,住址,,,)当输入该员工的id时,要求查找该员工的所有信息
要求:不适用数据库,尽量减少内存,速度越快越好==>哈希表(散列)
一、哈希表的基本介绍
散列表(Hash table,也叫哈希表)是根据关键码值(Key value)而直接进行访问的数据结构,也就是说它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度,这个映射函数叫做散列函数,存放记录的数组叫散列表。
代码实现:
import java.util.Scanner;
//有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,姓名,年龄,住址,,,)当输入该员工的id时,要求查找该员工的所有信息
//要求:不适用数据库,尽量减少内存,速度越快越好==>哈希表(散列)
public class HashTableDemo {
public static void main(String[] args) {
//创建一个哈希表
HashTable hashTable = new HashTable(7);
//写一个简单的菜单
String key = "";
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("add:添加雇员");
System.out.println("list:显示雇员");
System.out.println("select::查找雇员");
System.out.println("exit:退出系统");
key = scanner.next();
switch (key){
case "add":
System.out.println("请输入要加入的用户的id:");
int id = scanner.nextInt();
System.out.println("请输入要加入的用户的姓名:");
String name = scanner.next();
System.out.println("请输入要加入的用户的地址:");
String address = scanner.next();
Employee employee = new Employee(id,name,address);
hashTable.add(employee);
break;
case "list":
hashTable.list();
break;
case "select":
System.out.println("请输入要查询雇员id:");
hashTable.getEmployeeById(scanner.nextInt());
break;
case "exit":
scanner.close();
System.exit(0);
}
}
}
}
//表示一个雇员
class Employee{
private int id;
private String name;
private String address;
private Employee next;
public Employee(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Employee getNext() {
return next;
}
public void setNext(Employee next) {
this.next = next;
}
}
//创建一个EmployeeLinkedList,表示链表
class EmployeeLinkedList{
//头指针,执行第一个Employee,因此我们这个链表的head是直接指向第一个Employee
private Employee head; //默认为空
//添加雇员到链表中
//假定添加雇员时,id是自增长,即id的分配总是从小到大,因此我们直接将该雇员添加到链表的最后即可
public void addEmployee(Employee employee){
//如果是第一个雇员
if(head == null){
head = employee;
return;
}
//如果不是第一个雇员,则使用一个辅助的指针,帮助定位到最后
Employee curEmployee = head;
while(true){
if(curEmployee.getNext() == null){ //说明到链表最后
break;
}
curEmployee = curEmployee.getNext(); //后移
}
//退出时,直接将Employee加入到链表
curEmployee.setNext(employee);
}
//遍历链表的雇员信息
public void list(int no){
if(head == null){ //说明链表为空
System.out.println("第"+no+"条链表为空!");
return;
}
System.out.print("第"+no+"条链表为:");
Employee curEmployee = head;
while(true){
System.out.println("id="+curEmployee.getId()+",name="+curEmployee.getName()+",address="+curEmployee.getAddress());
if(curEmployee.getNext() == null){ //说明curEmployee已经是最后一个节点
break;
}
curEmployee.setNext(curEmployee);
}
// System.out.println();
}
//根据id查找雇员
public Employee getEmployeeById(int id){
if(head == null){
return null;
}
//辅助指针
Employee curEmployee = head;
while(true){
if(curEmployee.getId() == id){ //找到
break;
}
curEmployee.setNext(curEmployee);
if(curEmployee.getNext() == null){ //说明到达链表最后
curEmployee = null;
break;
}
}
return curEmployee;
}
}
//创建HashTable 管理多条链表
class HashTable{
private EmployeeLinkedList[] employeeLinkedListArray;
private int size;
//构造器
public HashTable(int size) {
this.size = size;
employeeLinkedListArray = new EmployeeLinkedList[size];
for(int i = 0; i < size; i++){
employeeLinkedListArray[i] = new EmployeeLinkedList();
}
}
//添加雇员
public void add(Employee employee){
//根据员工的id得到该员工应当添加到哪条链表
int employeeNo = hashFun(employee.getId());
//将employee添加到对应的链表中
employeeLinkedListArray[employeeNo].addEmployee(employee);
}
//遍历所有的链表,遍历HashTable
public void list(){
for(int i = 0; i < size; i++){
employeeLinkedListArray[i].list(i+1);
}
}
//根据id查找雇员
public void getEmployeeById(int id){
//使用散列函数确定到哪条链表查找
int employeeNo = hashFun(id);
Employee employee = employeeLinkedListArray[employeeNo].getEmployeeById(id);
if(employee != null){
System.out.println("在第"+(employeeNo+1)+"条链表找到雇员信息!");
}else {
System.out.println("没有id为"+id+"的雇员!");
}
}
//编写散列函数,使用一个简单取模法
public int hashFun(int id){
return id % size;
}
}
结果展示: