首頁  >  文章  >  Java  >  HashTable在Java中是如何運作的?

HashTable在Java中是如何運作的?

王林
王林轉載
2023-08-19 18:53:081107瀏覽

HashTable在Java中是如何運作的?

The Hashtable class is a part of the Java Collection Framework that stores its element in key-value pairs in a hash table. The Key is an object that can be used to fetch and receive value associated with it. There exist a few similarities between a Hashtable and HashMapclass but Hash table is synchronized. Also, its keys must be associated with values, they could not howulltable s works nartic s values, they could not becally s

Java中Hashtable的工作原理

我們可以將Hashtable視為一個桶的數組,每個桶包含一個條目列表。一個條目由鍵和值組成。我們指定一個鍵和一個可以與該鍵關聯的值。然後將鍵哈希化以產生雜湊碼,該雜湊碼進一步用作儲存值在表中的索引。幫助從雜湊碼獲取值位置的函數稱為雜湊函數。它總是傳回一個稱為雜湊碼的正整數值。多個物件在透過一個名為「equals()」的內建方法進行評估後可能會獲得相同的整數值。但是,相似的物件始終具有相同的雜湊碼。

Formula to allocate index

indexNumber = hashNumber % totalBuckets

Here, ‘%’ is the modulo operator that returns remainder

讓我們來舉個例子,展示上述公式的使用 -

Q. 假設我們得到了一個名為XYZ的元素的雜湊值為17,總數為 buckets available is 5. Then, find on which index number it will get stored?

解決方案 − 17 % 5 = 2 因此,它將獲得索引號2。

Collision in Hashtable

As discussed earlier, multiple objects might get same hashcode which leads to a situation called collision. It occurs when two or more keys have the same hash value and notsapped to snot same hashing notlow notkper perwheet notkper s fame not not, not cont. create any functional confusion.

宣告Hashtable的語法

Hashtable<TypeOfKey, TypeOfValue> nameOfTable = new Hashtable<>();

方法

  • 第一步是導入 'java.util' 套件,這樣我們可以使用 Hashtable 類別

  • #Define an instance of the Hashtable class and append some objects into it using a 內建的名為‘put()’的方法。

  • 現在,使用for-each循環,並在其中使用‘keySet()’方法來存取所有的鍵

  • 與鍵相關聯的值。

Example 1

The following example illustrates how we can implement a Hashtable in Java.

import java.util.*;
public class Table {
   public static void main(String[] args) {
      Hashtable<String, Integer> workers = new Hashtable<>();
      
      // Adding elements in the workers table
      workers.put("Vaibhav", 4000);
      workers.put("Ansh", 3000);
      workers.put("Vivek", 1500);
      workers.put("Aman", 2000);
      workers.put("Tapas", 2500);
      
      // printing details workers table
      System.out.println("Elements in the given table: ");
      for (String unKey : workers.keySet()) {
         System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey));
      }
   }
}

輸出

Elements in the given table:
Name: Aman, Salary: 2000
Name: Ansh, Salary: 3000
Name: Tapas, Salary: 2500
Name: Vivek, Salary: 1500
Name: Vaibhav, Salary: 4000

Example 2

In the following example, we will retrieve the values of a Hashtable by using the in-built method ‘get()’. This method accepts a key and returns the corresponding value.

import java.util.*;
public class Table {
   public static void main(String[] args) {
      Hashtable<String, Integer> workers = new Hashtable<>();
      
      // Adding elements in the workers table
      workers.put("Vaibhav", 4000);
      workers.put("Ansh", 3000);
      workers.put("Vivek", 1500);
      workers.put("Aman", 2000);
      workers.put("Tapas", 2500);
      
      // printing details workers table one by one
      System.out.println("Value stored at key Ansh: " + workers.get("Ansh"));
      System.out.println("Value stored at key Vivek: " + workers.get("Vivek"));
      System.out.println("Value stored at key Aman: " + workers.get("Aman"));
   }
}

輸出

Value stored at key Ansh: 3000
Value stored at key Vivek: 1500
Value stored at key Aman: 2000

結論

We started this article by defining the Hashtable class and in the next section, we explained how it works internally through an example. Later, we discussed the practical implementation of Hashtable through Java example programs.#mplementation of Hashtable through Java example programs. #mplementation

以上是HashTable在Java中是如何運作的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除