速读摘要文章源自JAVA秀-https://www.javaxiu.com/3995.html
数组和向量都可以存储对象,但对象的存储位置是随机的,也就是说对象本身与其存储位置之间没有必然的联系。之间建立一个特定的对应关系(设为f),使每个对象与一个唯一的存储位置相对应。如果此对象在集合中,则必定在存储位置f(k)上,因此不需要与集合中的其他元素进行比较。有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,住址..),当输入该员工的id时,要求查找到该员工的所有信息。文章源自JAVA秀-https://www.javaxiu.com/3995.html
原文约 2217 字 | 图片 10 张 | 建议阅读 5 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/3995.html
Google上机题:用Java代码实现哈希表。网友:太难了~
点击关注 ? Java面试那些事儿 文章源自JAVA秀-https://www.javaxiu.com/3995.html
点击关注下方公众号,Java面试资料 都在这里文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
作者:十四lin文章源自JAVA秀-https://www.javaxiu.com/3995.html
来源:https://www.cnblogs.com/linzm14/p/14495883.html文章源自JAVA秀-https://www.javaxiu.com/3995.html
正文如下:
文章源自JAVA秀-https://www.javaxiu.com/3995.html
# 什么是哈希表
文章源自JAVA秀-https://www.javaxiu.com/3995.html
数组和向量都可以存储对象,但对象的存储位置是随机的,也就是说对象本身与其存储位置之间没有必然的联系。当要查找一个对象时,只能以某种顺序(如顺序查找或二分查找)与各个元素进行比较,当数组或向量中的元素数量很多时,查找的效率会明显的降低。文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
一种有效的存储方式,是不与其他元素进行比较,一次存取便能得到所需要的记录。这就需要在对象的存储位置和对象的关键属性(设为 k)之间建立一个特定的对应关系(设为 f),使每个对象与一个唯一的存储位置相对应。在查找时,只要根据待查对象的关键属性 k 计算f(k)的值即可。如果此对象在集合中,则必定在存储位置 f(k)上,因此不需要与集合中的其他元素进行比较。称这种对应关系 f 为哈希(hash)方法,按照这种思想建立的表为哈希表。文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
# 哈希表(散列)-Google 上机题
文章源自JAVA秀-https://www.javaxiu.com/3995.html
1) 看一个实际需求,google 公司的一个上机题;文章源自JAVA秀-https://www.javaxiu.com/3995.html
2) 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的 id 时,要求查找到该员工的所有信息。文章源自JAVA秀-https://www.javaxiu.com/3995.html
3) 要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
# 哈希表的基本介绍
文章源自JAVA秀-https://www.javaxiu.com/3995.html
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
# google 公司的一个上机题
文章源自JAVA秀-https://www.javaxiu.com/3995.html
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址..),当输入该员工的 id 时,要求查找到该员工的 所有信息。文章源自JAVA秀-https://www.javaxiu.com/3995.html
要求:文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
1) 不使用数据库,,速度越快越好=>哈希表(散列)文章源自JAVA秀-https://www.javaxiu.com/3995.html
2) 添加时,保证按照 id 从低到高插入 文章源自JAVA秀-https://www.javaxiu.com/3995.html
3) 使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]文章源自JAVA秀-https://www.javaxiu.com/3995.html
4) 思路分析并画出示意图文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
5) 代码实现
文章源自JAVA秀-https://www.javaxiu.com/3995.html文章源自JAVA秀-https://www.javaxiu.com/3995.htmlpublic 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("find:查找雇员");
System.out.println("del:删除雇员");
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();
Emp emp = new Emp(id, name);
hashTable.add(emp);
break;
case "list":
hashTable.list();
break;
case "find":
System.out.println("请输入id:");
int id2 = scanner.nextInt();
hashTable.findEmpById(id2);
break;
case "del":
System.out.println("请输入id:");
int id3 = scanner.nextInt();
hashTable.del(id3);
break;
case "exit":
System.exit(10);
default:
break;
}
}
}
}
文章源自JAVA秀-https://www.javaxiu.com/3995.html// emp
class Emp{
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
文章源自JAVA秀-https://www.javaxiu.com/3995.html// EmpLinkedList
class EmpLinkedList{
// 头指针,执行第一个Emp,因此我们这个链表的head,是直接指向第一个Emp
private Emp head;
// id是自增长的
public void add(Emp emp) {
// 如果是添加一个雇员
if(head == null) {
head = emp;
return;
}
// 如果不是第一个
Emp curEmp = head;
while(true) {
if(curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
curEmp.next = emp;
}
public void list(int no) {
if(head == null) {
System.out.println("第" + (no+1) + "条链表为空!");
return;
}
System.out.println("第" + (no+1) + "条链表信息为:");
Emp curEmp = head;
while(true) {
System.out.printf("=> id=%d name=%s\t",curEmp.id,curEmp.name);
if(curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
System.out.println();
}
// 根据id查找雇员
public Emp findEmpByid(int id) {
if(head == null) {
System.out.println("链表为空");
return null;
}
Emp curEmp = head;
while(true) {
if(curEmp.id == id) {
break;
}
if(curEmp.next == null) {
System.out.println("遍历完了,没有找到!");
curEmp = null;
break;
}
curEmp = curEmp.next;
}
return curEmp;
}
// 根据id进行删除
public boolean del(int id) {
boolean flag = false;
if(head == null) {
System.out.println("当前链表为空!");
return flag;
}
if(head.id == id) {
head = null;
flag = true;
return flag;
}
Emp curEmp = head;
while(true) {
// 找到了改雇员
if(curEmp.next.id == id) {
curEmp.next = curEmp.next.next;
curEmp.next = null;
return (flag == false);
}
// 没有找到
if(curEmp.next == null) {
System.out.println("没有找改雇员!");
curEmp = null;
return flag;
}
curEmp = curEmp.next;
}
}
}
文章源自JAVA秀-https://www.javaxiu.com/3995.html// 哈希表
class HashTable{
private EmpLinkedList[] empLinkedListArr;
private int size;
public HashTable(int size) {
super();
this.size = size;
empLinkedListArr = new EmpLinkedList[size];
for(int i = 0; i < size; i++){
empLinkedListArr[i] = new EmpLinkedList();
}
}
// 添加雇员
public void add(Emp emp) {
// 根据员工的id得到改员工应该添加到哪条链表
int empLinkedListNo = hashFun(emp.id);
// 将emp添加到对应的链表中
empLinkedListArr[empLinkedListNo].add(emp);
}
public void list() {
for (int i = 0; i < empLinkedListArr.length; i++) {
empLinkedListArr[i].list(i);
}
}
public void findEmpById(int id) {
int empLinkedListNo = hashFun(id);
Emp emp = empLinkedListArr[empLinkedListNo].findEmpByid(id);
if(emp != null) {
System.out.println("在第" + (empLinkedListNo+1) + "条链表中找到id = " + id + "雇员");
} else {
System.out.println("在哈希表中没有找到");
}
}
public void del(int id) {
int empLinkedListNo = hashFun(id);
boolean flag = empLinkedListArr[empLinkedListNo].del(id);
if(flag == true) {
System.out.println("在第" + (empLinkedListNo+1) + "条链表中删除了id = " + id + "雇员");
} else {
System.out.println("在哈希表中没有找到");
}
}
public int hashFun(int id) {
return id %size;
}
}
文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
注意:不要把链表的第一个节点(头节点)删除了,不然整条链表没了。(还可以改良)文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
思考:如果 id 不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
文章源自JAVA秀-https://www.javaxiu.com/3995.html
热门推荐:D哥常用的轻量化哈希校验工具,右键菜单栏一键计算文件Hash~Tencent Kona JDK11 来了!腾讯宣布正式开源为啥大佬们都说安全框架Shiro的设计相当精妙?它底层是如何实现的?文章源自JAVA秀-https://www.javaxiu.com/3995.html

评论