溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

講解Java 哈希表(google 公司的上機(jī)題)

發(fā)布時(shí)間:2021-03-08 13:42:18 來(lái)源:億速云 閱讀:191 作者:TREX 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“講解Java 哈希表(google 公司的上機(jī)題)”,在日常操作中,相信很多人在講解Java 哈希表(google 公司的上機(jī)題)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”講解Java 哈希表(google 公司的上機(jī)題)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

1 哈希表(散列)-Google 上機(jī)題

1) 看一個(gè)實(shí)際需求,google 公司的一個(gè)上機(jī)題:
2) 有一個(gè)公司,當(dāng)有新的員工來(lái)報(bào)道時(shí),要求將該員工的信息加入(id,性別,年齡,住址..),當(dāng)輸入該員工的 id 時(shí),要求查
找到該員工的 所有信息.
3) 要求: 不使用數(shù)據(jù)庫(kù),盡量節(jié)省內(nèi)存,速度越快越好=>哈希表(散列)

2 哈希表的基本介紹

散列表(Hash table,也叫哈希表),是根據(jù)關(guān)鍵碼值(Key value)而直接進(jìn)行訪問(wèn)的數(shù)據(jù)結(jié)構(gòu)。也就是說(shuō),它通
過(guò)把關(guān)鍵碼值映射到表中一個(gè)位置來(lái)訪問(wèn)記錄,以加快查找的速度。這個(gè)映射函數(shù)叫做散列函數(shù),存放記錄的數(shù)組
叫做散列表。

講解Java 哈希表(google 公司的上機(jī)題)

3. google 公司的一個(gè)上機(jī)題:

有一個(gè)公司,當(dāng)有新的員工來(lái)報(bào)道時(shí),要求將該員工的信息加入(id,性別,年齡,名字,住址..),當(dāng)輸入該員工的 id 時(shí),
要求查找到該員工的 所有信息.
要求:
  1) 不使用數(shù)據(jù)庫(kù),,速度越快越好=>哈希表(散列)
  2) 添加時(shí),保證按照 id 從低到高插入
  3) 使用鏈表來(lái)實(shí)現(xiàn)哈希表, 該鏈表不帶表頭[即: 鏈表的第一個(gè)結(jié)點(diǎn)就存放雇員信息]
  4) 思路分析并畫出示意圖

講解Java 哈希表(google 公司的上機(jī)題)

  5) 代碼實(shí)現(xiàn)

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("請(qǐng)輸入id:");
        int id = scanner.nextInt();
        System.out.println("請(qǐng)輸入名字:");
        String name = scanner.next();
        
        Emp emp = new Emp(id, name);
        hashTable.add(emp);
        break;
      case "list":
        hashTable.list();
        break;
      case "find":
        System.out.println("請(qǐng)輸入id:");
        int id2 = scanner.nextInt();
        hashTable.findEmpById(id2);
        break;
      case "del":
        System.out.println("請(qǐng)輸入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{
  // 頭指針,執(zhí)行第一個(gè)Emp,因此我們這個(gè)鏈表的head,是直接指向第一個(gè)Emp
  private Emp head;
  
  // id是自增長(zhǎng)的
  public void add(Emp emp) {
    // 如果是添加一個(gè)雇員
    if(head == null) {
      head = emp;
      return;
    }
    // 如果不是第一個(gè)
    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();
  }
  
  // 根據(jù)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("遍歷完了,沒(méi)有找到!");
        curEmp = null;
        break;
      }
      curEmp = curEmp.next;
    }
    return curEmp;
  }
  
  // 根據(jù)id進(jìn)行刪除
  public boolean del(int id) {
    boolean flag = false;
    if(head == null) {
      System.out.println("當(dāng)前鏈表為空!");
      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);
      }
      // 沒(méi)有找到
      if(curEmp.next == null) {
        System.out.println("沒(méi)有找改雇員!");
        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) {
    // 根據(jù)員工的id得到改員工應(yīng)該添加到哪條鏈表
    int empLinkedListNo = hashFun(emp.id);
    // 將emp添加到對(duì)應(yīng)的鏈表中
    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("在哈希表中沒(méi)有找到");
    }
  }
  
  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("在哈希表中沒(méi)有找到");
    }
    
  }
  
  public int hashFun(int id) {
    return id %size;
  }
}

講解Java 哈希表(google 公司的上機(jī)題)

講解Java 哈希表(google 公司的上機(jī)題)

注意:不要把鏈表的第一個(gè)節(jié)點(diǎn)(頭節(jié)點(diǎn))刪除了,不然整條鏈表沒(méi)了。(還可以改良)

思考:如果 id 不是從低到高插入,但要求各條鏈表仍是從低到高,怎么解決?

到此,關(guān)于“講解Java 哈希表(google 公司的上機(jī)題)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI