/**
* @author chenyi
* @Description 自定义hashtable保存员工信息
* @date 2022/2/17 11:12
*/
public class MyHashtable {
int capacity = 11;
private Node[] data;
public MyHashtable() {
data = new Node[11];
}
private int hash(int id) {
return id % capacity;
}
public void add(Employee employee) {
int hashCode = hash(employee.getId());
Node newNode = new Node(employee);
Node head = data[hashCode];
if (head == null) {
data[hashCode] = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public Employee find(int id) {
int hashCode = hash(id);
Node curr = data[hashCode];
while (curr != null) {
if (curr.getEmployee()
.getId() == id) {
return curr.getEmployee();
}
curr = curr.next;
}
return null;
}
public void list() {
for (int i = 0; i < data.length; i++) {
Node curr = data[i];
System.out.print("第" + (i + 1) + "条链表:");
while (curr != null) {
System.out.print(curr.getEmployee()
.toString());
curr = curr.next;
}
System.out.println();
}
}
private Employee remove(int id) {
int hashCode = hash(id);
Node curr = data[hashCode];
Node prev = null;
while (curr != null) {
if (curr.getEmployee()
.getId() == id) {
if(prev==null) {
data[hashCode] = curr.next;
}else{
prev.next = curr.next;
}
curr.next = null;
return curr.employee;
}
prev = curr;
curr = curr.next;
}
return null;
}
class Node {
private Employee employee;
private Node next;
public Node(Employee employee) {
this.employee = employee;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Employee getEmployee() {
return employee;
}
}
public static void main(String[] args) {
MyHashtable myHashtable = new MyHashtable();
// 添加员工
System.out.println("添加测试");
myHashtable.add(new Employee(0,"jet"));
myHashtable.add(new Employee(1,"jack"));
myHashtable.add(new Employee(2,"smith"));
myHashtable.add(new Employee(3,"jane"));
myHashtable.add(new Employee(4,"jerry"));
myHashtable.add(new Employee(11,"hover"));
myHashtable.list();
System.out.println("删除测试");
myHashtable.remove(0);
myHashtable.list();
myHashtable.remove(0);
myHashtable.list();
myHashtable.remove(11);
myHashtable.list();
System.out.println("查询测试");
Employee employee = myHashtable.find(4);
System.out.println(employee);
}
}
class Employee {
private int id;
private String name;
public Employee(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 "{" + "id=" + id + ", name='" + name + '\'' + '}';
}
}
自定义hash表实现员工保存
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 适合场景 在使用Spring Data JPA的时候,在使用UUID作为主键的时候,通常都会使用system-uu...
- 在开发中,会经常遇到拷贝数据对象的情况,也会遇到保存数据到本地的情况,如果是用OC的话,无论是copyWithZo...
- python 日志模块 logging FileHandler: 以“a”(追加)的方式将日志输出到文件,如果文件...
- 在上篇中,我与大家分享了关于如何进行*.lrc歌词文件的解析,以及将解析完成后的歌词展示在镶嵌在ScrollVie...