如何在 TypeScript 中使用雜湊表?
雜湊表是一種儲存不同資料鍵值對的資料結構。與其他程式語言一樣,TypeScript也包含一個內建的對映資料結構。在JavaScript中,我們無法定義需要儲存在對映中的鍵或值的型別。因此,我們需要建立一個泛型型別的對映。在TypeScript中,我們可以定義儲存在對映中的鍵和值的型別。
語法
以下是 TypeScript 中建立對映的語法:
let hashMap = new Map<Key_Type, value_Type>();
引數
key_Type - 它是鍵的資料型別。使用者可以使用字串、數字、布林值、物件、陣列等。
value_Type - 它是值的資料型別。
TypeScript 中的雜湊表方法
TypeScript 的對映類包含 5 到 6 個方法,我們可以透過以任何對映類物件作為引用來呼叫這些方法。使用者可以在下面看到每個方法的解釋。
get(key) - 對映類的 get() 方法用於獲取對映中特定鍵儲存的值。雜湊表包含唯一的鍵,我們可以使用 get() 方法訪問其值。
let hashMap = new Map<number, string>(); hashmap.get(1);
set(key, value) - 對映類的 set() 方法允許我們在對映中設定鍵值對。它將鍵作為第一個引數,將值作為第二個引數。此外,它採用唯一的鍵。如果使用者傳遞現有鍵,則 set() 方法將替換現有鍵的值。
let hashMap = new Map<number, string>(); hashMap.set(1,"TutorialsPoint");
delete(key) - delete() 方法將鍵作為引數,並從對映物件中刪除鍵及其對映的值。
let hashMap = new Map<number, string>(); hashMap.delete(1);
has(key) - has() 方法也以鍵作為引數,並檢查雜湊表是否包含特定鍵及其對映的值。
let hashMap = new Map<number, string>(); hashMap.has(1);
Size - 大小是對映類的一個變數,包含表示雜湊表包含的鍵值對總數的數值。
let hashMap = new Map<number, string>(); let total = hashMap.size;
clear() - clear() 方法從 map() 物件中刪除所有鍵值對,並將其大小設定為 0。
let hashMap = new Map<number, string>(); hashMap.clear();
示例
在下面的示例中,我們建立了一個對映並將其儲存在“hashmap”中。之後,我們使用 set() 方法在 hashmap 中設定了 7 個不同的鍵值對。接下來,我們使用 get() 方法訪問與特定鍵相關的值。
// Creating the new hashmap. Data type of the key
// is number and data type of the value is string.
let hashmap = new Map<Number, string>();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the values from the hashmap
let value1: string | undefined = hashmap.get(1);
// If hashmap doesn't contains key-value pair, it returns the u
//ndefined that's why we need to set type of value1 variable to undefined.
console.log("Value for the key 1 is " + value1);
let value2: string | undefined = hashmap.get(2);
console.log("Value for the key 2 is " + value2);
編譯後,它將生成以下 JavaScript 程式碼:
// Creating the new hashmap. Data type of the key
// is number and data type of the value is string.
var hashmap = new Map();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the values from the hashmap
var value1 = hashmap.get(1);
// If hashmap doesn't contains key-value pair, it returns the u
//ndefined that's why we need to set type of value1 variable to undefined.
console.log("Value for the key 1 is " + value1);
var value2 = hashmap.get(2);
console.log("Value for the key 2 is " + value2);
輸出
以上程式碼將產生以下輸出:
Value for the key 1 is Hello Value for the key 2 is World!
示例
在下面的示例中,我們建立了一個對映並將其儲存在“hashmap”中,並獲取 hashmap 的大小。要獲取大小,我們使用對映類的“size”變數。
let hashmap = new Map<Number, string>();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the size of hashmap
console.log(" ");
console.log("The size of the hashmap is " + hashmap.size);
編譯後,它將生成以下 JavaScript 程式碼:
var hashmap = new Map();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the size of hashmap
console.log(" ");
console.log("The size of the hashmap is " + hashmap.size);
輸出
以上程式碼將產生以下輸出:
The size of the hashmap is 2
示例
之後,我們使用 has() 方法檢查特定鍵是否存在於 hashmap 中。
let hashmap = new Map<Number, string>();
hashmap.set(1, "Welcome back");
hashmap.set(2, "to Tutorials Point");
console.log("hashmap contains the key 1? " + hashmap.has(1));
// returns true as it contains key 1.
console.log("hashmap contains the key 3? " + hashmap.has(3));
// returns false as the key 3 does not exist.
編譯後,它將生成以下 JavaScript 程式碼:
var hashmap = new Map();
hashmap.set(1, "Welcome back");
hashmap.set(2, "to Tutorials Point");
console.log("hashmap contains the key 1? " + hashmap.has(1));
// returns true as it contains key 1.
console.log("hashmap contains the key 3? " + hashmap.has(3));
// returns false as the key 3 does not exist.
輸出
以上程式碼將產生以下輸出:
hashmap contains the key 1? true hashmap contains the key 3? false
示例
在此示例中,我們從 hashmap 中刪除了兩個鍵值對。我們還使用 clear() 方法刪除所有對映記錄。
// Creating the new hashmap.
let hashmap = new Map<Number, string>();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Welcome");
hashmap.set(2, "To Tutorials Point");
// delete the key 2
hashmap.delete(2);
console.log("Hashmap contains the key 2 ? " + hashmap.has(2));
// returns false as we deleted the key 2.
// Clear the hashmap
console.log(" ");
hashmap.clear();
console.log("The size of the hashmap after clearing is :");
console.log(hashmap.size);
// It returns 0 as we cleared the whole hashmap.
編譯後,它將生成以下 JavaScript 程式碼:
// Creating the new hashmap.
var hashmap = new Map();
// Setting up number-string pairs into the hashmap.
hashmap.set(1, "Welcome");
hashmap.set(2, "To Tutorials Point");
// delete the key 2
hashmap["delete"](2);
console.log("Hashmap contains the key 2 ? " + hashmap.has(2));
// returns false as we deleted the key 2.
// Clear the hashmap
console.log(" ");
hashmap.clear();
console.log("The size of the hashmap after clearing is :");
console.log(hashmap.size);
// It returns 0 as we cleared the whole hashmap.
輸出
以上程式碼將產生以下輸出:
Hashmap contains the key 2 ? false The size of the hashmap after clearing is : 0
在本教程中,使用者學習了雜湊表的用法。透過以上示例,使用者學習瞭如何在 TypeScript 中使用雜湊表的方法。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP