如何在 TypeScript 中使用 Hashmap?
Hashmap 是一种用于存储不同数据的键值对的数据结构。与其他编程语言一样,TypeScript 也包含一个内置的 Map 数据结构。在 JavaScript 中,我们无法定义需要存储在 Map 中的键或值的类型。因此,我们需要创建一个泛型类型的 Map。在 TypeScript 中,我们可以定义要存储在 Map 中的键和值的类型。
语法
以下是在 TypeScript 中创建 Map 的语法 -
let hashMap = new Map<Key_Type, value_Type>();
参数
key_Type − 键的数据类型。用户可以使用字符串、数字、布尔值、对象、数组等。
value_Type − 值的数据类型。
TypeScript 中的 HashMap 方法
TypeScript 的 Map 类包含 5 到 6 个方法,我们可以通过引用 Map 类的任何对象来调用这些方法。用户可以在下面查看每个方法的说明。
get(key) − Map 类的 get() 方法用于获取 Map 中存储的特定键的值。 hashmap 包含唯一键,我们可以使用 get() 方法访问其值。
let hashMap = new Map<number, string>(); hashmap.get(1);
set(key, value) − map 类的 set() 方法允许我们在 map 中设置键值对。该方法将键作为第一个参数,将值作为第二个参数。此外,该方法还接受一个唯一键。如果用户传递现有键,set() 方法将替换现有键的值。
let hashMap = new Map<number, string>(); hashMap.set(1,"TutorialsPoint");
delete(key) − delete() 方法以键为参数,从 Map 对象中删除该键及其映射值。
let hashMap = new Map<number, string>(); hashMap.delete(1);
has(key) − has() 方法同样以键为参数,检查 HashMap 是否包含该键及其映射值。
let hashMap = new Map<number, string>(); hashMap.has(1);
Size − size 是 map 类的一个变量,包含一个数值,表示 hashmap 所包含的键值对的总数。
let hashMap = new Map<number, string>(); let total = hashMap.size;
clear() − clear() 方法从 map() 对象中删除所有键值对,并将其大小设置为 0。
let hashMap = new Map<number, string>(); hashMap.clear();
示例
在下面的示例中,我们创建了一个 Map 并将其存储在"hashmap"中。之后,我们使用 set() 方法设置了 Hashmap 中的 7 个不同的键值对。接下来,我们使用 get() 方法访问与特定键相关的值。
// 创建新的 Hashmap。键的数据类型为 // 数字,值的数据类型为字符串。 let hashmap = new Map<Number, string>(); // 将数字-字符串对设置到 Hashmap 中。 hashmap.set(1, "Hello"); hashmap.set(2, "World!"); // 从 Hashmap 中获取值 let value1: string | undefined = hashmap.get(1); // 如果 hashmap 不包含键值对,则返回 u //ndefined,因此我们需要将 value1 变量的类型设置为 undefined。 console.log("键 1 的值为 " + value1); let value2: string | undefined = hashmap.get(2); console.log("键 2 的值为 " + value2);
编译后,它负责生成以下 JavaScript 代码 -
// 创建新的 hashmap。键的数据类型为 // 数字,值的数据类型为字符串。 var hashmap = new Map(); // 将数字-字符串对设置到 hashmap 中。 hashmap.set(1, "Hello"); hashmap.set(2, "World!"); // 从 hashmap 中获取值 var value1 = hashmap.get(1); // 如果 hashmap 不包含键值对,则返回 u //ndefined,这就是为什么我们需要将 value1 变量的类型设置为 undefined。 console.log("键 1 的值为 " + value1); var value2 = hashmap.get(2); console.log("键 2 的值为 " + value2);
输出
上述代码将产生以下输出 -
键 1 的值为 Hello 键 2 的值为 World!
示例
在下面的示例中,我们创建一个 Map 并将其存储在"hashmap"中,并获取该 Map 的大小。为了获取大小,我们使用 Map 类的"size"变量。
let hashmap = new Map(); // 将数字-字符串对设置到 hashmap 中。 hashmap.set(1, "Hello"); hashmap.set(2, "World!"); // 获取 hashmap 的大小 console.log(" "); console.log("hashmap 的大小为 " + hashmap.size);
编译后,它负责生成以下 JavaScript 代码 -
var hashmap = new Map(); // 将数字-字符串对设置到哈希图中。 hashmap.set(1, "Hello"); hashmap.set(2, "World!"); // 获取哈希图的大小 console.log(" "); console.log("哈希图的大小为 " + hashmap.size);
输出
上述代码将产生以下输出 -
The size of the hashmap is 2
示例
之后,我们使用 has() 方法来检查特定键是否存在于哈希图中。
let hashmap = new Map<Number, string>(); hashmap.set(1, "Welcome back"); hashmap.set(2, "to Tutorials Point"); console.log("hashmap 包含键 1 吗?" + hashmap.has(1)); // 返回 true,因为它包含键 1。 console.log("hashmap 包含键 3 吗?" + hashmap.has(3)); // 返回 false,因为键 3 不存在。
编译后,它将生成以下 JavaScript 代码 -
var hashmap = new Map(); hashmap.set(1, "Welcome back"); hashmap.set(2, "to Tutorials Point"); console.log("hashmap 包含键 1 吗?" + hashmap.has(1)); // 返回 true,因为它包含键 1。 console.log("hashmap 包含键 3 吗?" + hashmap.has(3)); // 返回 false,因为键 3 不存在。
输出
上述代码将产生以下输出 -
hashmap 包含键 1 吗?true hashmap 包含键 3 吗?false
示例
在此示例中,我们从 hashmap 中删除两个键值对。我们还使用 clear() 方法删除了所有 map 记录。
// 创建新的 hashmap。 let hashmap = new Map<Number, string>(); // 将数字-字符串对设置到 hashmap 中。 hashmap.set(1, "Welcome"); hashmap.set(2, "To Tutorials Point"); // 删除键 2 hashmap.delete(2); console.log("Hashmap 包含键 2 吗?" + hashmap.has(2)); // 返回 false,因为我们删除了键 2。 // 清除 hashmap console.log(" "); hashmap.clear(); console.log("清除后 hashmap 的大小为:"); console.log(hashmap.size); // 返回 0,因为我们清除了整个 hashmap。
编译后,它将生成以下 JavaScript 代码 -
// 创建新的哈希表。 var hashmap = new Map(); // 将数字-字符串对设置到哈希表中。 hashmap.set(1, "Welcome"); hashmap.set(2, "To Tutorials Point"); // 删除键 2 hashmap["delete"](2); console.log("哈希表包含键 2 吗?" + hashmap.has(2)); // 返回 false,因为我们删除了键 2。 // 清除哈希表 console.log(" "); hashmap.clear(); console.log("清除后哈希表的大小为:"); console.log(hashmap.size); // 返回 0,因为我们清除了整个哈希表。
输出
上述代码将产生以下输出 -
Hashmap 包含键 2 ?false 清除后 Hashmap 的大小为: 0
在本教程中,用户学习了 Hashmap 的基本用法。通过上面的示例,用户学习了在 TypeScript 中使用 Hashmap 的方法。