讲解Java 哈希表(google 公司的上机题)

本文主要介绍"讲解Java 哈希表(google 公司的上机题)",希望能够解决您遇到有关问题,下面我们一起来看这篇 "讲解Java 哈希表(google 公司的上机题)" 文章。

1 哈希表(散列)-Google 上机题

1) 看一个实际需求,google 公司的一个上机题:
2) 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的 id 时,要求查
找到该员工的 所有信息.
3) 要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)

2 哈希表的基本介绍

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通
过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组
叫做散列表。

讲解Java 哈希表(google 公司的上机题)

3. google 公司的一个上机题:

有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址..),当输入该员工的 id 时,
要求查找到该员工的 所有信息.
要求:
  1) 不使用数据库,,速度越快越好=>哈希表(散列)
  2) 添加时,保证按照 id 从低到高插入
  3) 使用链表来实现哈希表, 该链表不带表头[即: 链表的第一个结点就存放雇员信息]
  4) 思路分析并画出示意图

讲解Java 哈希表(google 公司的上机题)

  5) 代码实现

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("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;
      }
    }
  }
}
// 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;
  }
}
// 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;
    }
  }
}
// 哈希表
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 哈希表(google 公司的上机题)

讲解Java 哈希表(google 公司的上机题)

注意:不要把链表的第一个节点(头节点)删除了,不然整条链表没了。(还可以改良)

思考:如果 id 不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?

关于 "讲解Java 哈希表(google 公司的上机题)" 就介绍到这。希望大家多多支持编程宝库

本文主要介绍"Java8新特性之方法引用的使用方法",希望能够解决您遇到有关问题,下面我们一起来看这篇 "Java8新特性之方法引用的使用方法" 文章。 一 前言日常开发中,经常使用到 ...