数组链表实现的哈希表
题目:有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,姓名,年龄),当输入员工id时,要求查找该员工的所有信息。
要求:
- 不使用数据库,速度越快越好------> 哈希表(散列表)
图片显示
代码实现如下:
Emp类
package com.hashtable;
public class Emp {
private int id ;
private String name;
private int age ;
private Emp next ;
public Emp() {
}
public Emp(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public Emp getNext() {
return this.next;
}
public void setNext(Emp next) {
this.next = next;
}
@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", name='" + getName() + "'" +
", age='" + getAge() + "'" +
"}";
}
}
EmpLinkedList类
package com.hashtable;
import java.util.ArrayList;
public class EmpLinkedList {
private Emp head ;
//添加Emp到list
public void addByTail(Emp emp){
//说明添加第一个
if(head == null){
head = emp ;
return ;
}
//不是第一个添加,就需要找到结尾
Emp temp = head ;
while(true){
if(temp.getNext() == null){
break ;
}
}
temp.setNext(emp);
}
//遍历员工的信息
public ArrayList<Emp> showList(){
if(head == null){
try {
throw new Exception("本条链表为空");
} catch (Exception e) {
e.printStackTrace();
}
}
Emp temp = head ;
ArrayList<Emp> list = new ArrayList<Emp>() ;
while(true){
if(temp == null){
break ;
}
list.add(temp);
temp = temp.getNext() ;
}
return list ;
}
//根据id查找员工信息
public Emp findEmpById(int id){
Emp temp = head ;
while(true){
if(temp == null){
break ;
}
if(temp.getId() == id){
return temp ;
}
temp = temp.getNext() ;
}
return null ;
}
}
HashTab类
package com.hashtable;
import java.util.ArrayList;
public class HashTab {
private int maxSize ;
private EmpLinkedList[] empLinkedLists ;
public HashTab(int maxSize){
this.maxSize = maxSize ;
empLinkedLists = new EmpLinkedList[maxSize] ;
//千万记得初始化每个链表
for (int i = 0; i < maxSize; i++) {
empLinkedLists[i] = new EmpLinkedList() ;
}
}
//散列函数 取模的形式
public int hashFun(int id){
return id % maxSize ;
}
//添加成员
public void add(Emp emp){
int id = emp.getId() ;
empLinkedLists[hashFun(id)].addByTail(emp);
}
//遍历成员
public ArrayList<Emp> showList(){
ArrayList<Emp> list = new ArrayList<Emp>() ;
for (int i = 0; i < maxSize; i++) {
list.addAll(empLinkedLists[i].showList());
}
return list ;
}
//通过id获取成员信息
public Emp getEmpById(int id){
int no = hashFun(id) ;
Emp e = empLinkedLists[no].findEmpById(id) ;
return e;
}
}
测试类:
package com.hashtable;
import java.util.ArrayList;
public class HashTabTest {
public static void main(String[] args){
Emp e1 = new Emp(1, "zhangsan", 23);
Emp e2 = new Emp(2, "lisi", 24);
Emp e3 = new Emp(13, "wangwu", 25);
Emp e4 = new Emp(4, "zhaoliu", 23);
Emp e5 = new Emp(9, "xiaoqi", 27);
HashTab hTab = new HashTab(7) ;
hTab.add(e1);
hTab.add(e2);
hTab.add(e3);
hTab.add(e4);
hTab.add(e5);
ArrayList<Emp> list = hTab.showList() ;
System.out.println(list);
System.out.println(hTab.getEmpById(13) );
}
}
扫描二维码关注公众号,回复:
10654800 查看本文章