• java哈希表(含校园系统散列表的代码)


    目录

    一:哈希表的思想

    二:开发地址法

    三:链地址法(HashMap)

    四:链地址法哈希表代码

    4.1 汽车结点的定义

    4.2 汽车链表的定义

    4.3 哈希表的设计

    4.4 测试


    一:哈希表的思想

    哈希碰撞:数据丢失

     

    避免哈希碰撞

    散列函数选好,混匀分配

    key极小的变化引起哈希值较大的变化

    二:开发地址法

    当值传进来的时候,通过散列函数得到哈希值(数组下标),当改下标存在数值的时候就需要哈希值+1,如下图(5 -> 6 -> 7 -> 8);

    当内存不够了,可设计为循环数组(可参考我前面的循环链表),当没有空间的时候没办法插入值,此时需要扩充内存,势必浪费空间

    三:链地址法(HashMap

    数组+链表

    如果出现哈希冲突,就地生成一个链表

    四:链地址法哈希表代码

    4.1 汽车结点的定义

    1. package hashmap;
    2. public class Car {
    3. private int id;
    4. private String name;
    5. private Car next;
    6. public Car(int id, String name) {
    7. this.id = id;
    8. this.name = name;
    9. }
    10. public void setId(int id) {
    11. this.id = id;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public int getId() {
    17. return this.id;
    18. }
    19. public String getName() {
    20. return this.name;
    21. }
    22. public void setNext(Car next) {
    23. this.next = next;
    24. }
    25. public Car getNext() {
    26. return this.next;
    27. }
    28. }

    4.2 汽车链表的定义

    1. package hashmap;
    2. public class CarList {
    3. private Car head;
    4. //添加汽车
    5. public void add(Car car) {
    6. if(head == null) {
    7. head = car;
    8. return;
    9. }
    10. Car temp = head;
    11. while(true) {
    12. if(temp.getNext() == null) {
    13. break;
    14. }
    15. temp = temp.getNext();
    16. }
    17. temp.setNext(car);
    18. }
    19. //查找汽车链表
    20. public void search(int no) {
    21. if(head == null) {
    22. System.out.printf("第%d个链表为空...\n", no);
    23. return;
    24. }
    25. Car temp = head;
    26. while(true) {
    27. System.out.printf("第%d个链表:汽车编号 = %d, 汽车名字 = %s\n",no, temp.getId(), temp.getName());
    28. if(temp.getNext() == null) {
    29. break;
    30. }
    31. temp = temp.getNext();
    32. }
    33. }
    34. //查找汽车编号
    35. public Car searchId(int id) {
    36. if(head == null) {
    37. System.out.println("汽车链表为空...");
    38. return null;
    39. }
    40. Car temp = head;
    41. while(true) {
    42. if(temp.getId() == id) {
    43. break;
    44. }
    45. if(temp.getNext() == null) {
    46. temp = null;
    47. break;
    48. }
    49. temp = temp.getNext();
    50. }
    51. return temp;
    52. }
    53. }

    4.3  哈希表的设计

    1. package hashmap;
    2. public class HashArr {
    3. private int max;
    4. private CarList[] cl;
    5. public HashArr(int max) {
    6. this.max = max;
    7. cl = new CarList[max];
    8. for(int i = 0; i < max; i++) {
    9. cl[i] = new CarList();
    10. }
    11. }
    12. //散列函数
    13. private int HashCodes(int id) {
    14. return id%max;
    15. }
    16. //添加
    17. public void add(Car car) {
    18. int val = HashCodes(car.getId());
    19. cl[val].add(car);
    20. }
    21. //查看链表
    22. public void list() {
    23. for(int i = 0; i < cl.length; i++) {
    24. cl[i].search(i);
    25. }
    26. }
    27. //按编号查看
    28. public void listId(int id) {
    29. int val = HashCodes(id);
    30. Car c = cl[val].searchId(id);
    31. if(c == null) {
    32. System.out.println("该汽车不存在...");
    33. }
    34. if(c != null) {
    35. System.out.printf("在第%d条链表中找到了该车:编号 = %d,姓名=%s\n", val, c.getId(), c.getName());
    36. }
    37. }
    38. }

    4.4  测试

    1. package hashmap;
    2. public class Test {
    3. public static void main(String[] args) {
    4. //添加
    5. Car car1 = new Car(1,"大众");
    6. Car car2 = new Car(2,"宝马");
    7. Car car3 = new Car(3,"奥迪");
    8. Car car4 = new Car(4,"帕萨特");
    9. Car car5 = new Car(10,"凯迪拉克");
    10. HashArr arr = new HashArr(6);
    11. arr.add(car1);
    12. arr.add(car2);
    13. arr.add(car3);
    14. arr.add(car4);
    15. arr.add(car5);
    16. arr.list();
    17. arr.listId(10);
    18. }
    19. }

  • 相关阅读:
    基于蝴蝶优化的BP神经网络(分类应用) - 附代码
    微信⼩程序实现限制⽤户转发功能的实例代码
    Docker学习
    Linux web访问日志在什么目录
    Vue基础-04
    Java容器详解(浅层)
    Python基础(6-1)函数
    【Python高级编程】Matplotlib 绘图中文显示问题与常见错误合集
    探索 SOCKS5 代理在跨境电商中的网络安全应用
    线程状态及线程之间通信
  • 原文地址:https://blog.csdn.net/qq_56127002/article/details/126442141