在哈希表(HashMap)中查找具有给定键的元素,并返回对应的值。
def find(self, key: Key) -> Optional[Value]
key
:要查找的键值(Key类型)。None
。# 创建哈希表
hash_map = core.HashMap[int, str]()
# 插入元素
hash_map.insert(1, "apple")
hash_map.insert(2, "banana")
hash_map.insert(3, "orange")
# 查找键值为2的元素
value = hash_map.find(2)
if value:
print(value) # 输出:"banana"
# 查找键值为4的元素
value = hash_map.find(4)
if not value:
print("Not found!") # 输出:"Not found!"