This is a HashMap
implementation written in Python. All the methods are based on the java implementation of a HashMap
. Any and all critique is welcome.
This implementation is just a rewrite of some java HashMap
methods. Yes, it's basically a wrapper class for pythons dict
. This implementation is a simple exercise to see if I could write something from Java in Python.
from typing import Union, List
class HashMap:
def __init__(self):
self.capacity = 16
self.map = {}
def clear(self) -> None:
"""
Clears all the entries into this hashmap
:return: None
"""
self.map = {}
def contains_key(self, key: object) -> bool:
"""
Returns if the map contains the passed key
:param key -> object: Value to check residency in map keyset
:return bool: True if "key" in map keyset, False otherwise
"""
return key in self.map.keys()
def contains_value(self, value: object) -> bool:
"""
Returns if the map contains the passed value
:param value -> object: Value to check residency in map valueset
:return bool: True if "value" in map valueset, False otherwise
"""
return value in self.map.values()
def entry_set(self) -> set:
"""
Returns a set of the hashmap
:return set: A set representation of the map
"""
return set(self.map)
def get(self, key: object) -> Union[object, None]:
"""
Returns the value at the passed key, None if not present
:param key -> object: Key to retrieve value in map
:return Union[object, None]: value at "key", None if key is not present in map
"""
return self.map[key] if key in self.map.keys() else None
def get_or_default(self, key: object, default_value: object) -> object:
"""
Returns the value at the passed key, or default_value if not present
:param key -> object: Key to retrieve value in map\n
:param default_value -> object: Value to return if key is not present in map
:return object: Value associated with "key", "default_value" otherwise
"""
return self.map[key] if key in self.map.keys() else default_value
def is_empty(self) -> bool:
"""
Returns if the map has no key-value entries
:return bool: True if map isn't empty, False otherwise
"""
return self.map != {}
def key_set(self) -> set:
"""
Returns a set of all the keys
:return set: Set of all keys in map
"""
return set(self.map.keys())
def put(self, key: object, value: object) -> object:
"""
Adds the key-value pair to the map, returning the value
:param key -> object: Key to add to set\n
:param value -> object: Value to add to set
:return object: "value" passed
"""
self.map[key] = value
return value
def remove(self, key: object) -> Union[object, None]:
"""
Removes the mapping for the passed key, returning the value
:param key -> object: Key to retrieve value from map
:return object: Value associated with "key", None if key not in map keyset
"""
if key in self.map.keys():
value = self.map[key]
del self.map[key]
return value
return None
def size(self) -> int:
"""
Returns the size of the hashmap
:return int: Size of map
"""
return len(self.map)
def values(self) -> List[object]:
"""
Returns a list of the values in the hashmap
:return List[object]: List of values in map
"""
return list(self.map.values())
Testing Implementation
if __name__ == "__main__":
hashmap = HashMap()
hashmap.put("Ben", 18)
hashmap.put(5, 18)
hashmap.put("5", True)
hashmap.put(False, 3.661)
print(hashmap.get(5))
print(hashmap.get(False))
print(hashmap.get("Ben"))
print(hashmap.size())
print(hashmap.values())
capacity
attribute which I assume would have some sort of meaning in Java-land), or is it something else? \$\endgroup\$dict
provides". \$\endgroup\$