def test6(cus_map: HashMap, test_unit): cus_map.put("jack", 1) cus_map.put("rose", 2) cus_map.put("jim", 3) cus_map.put("jake", 4) for i in range(1, 11): cus_map.put("test" + str(i), i) cus_map.put(Key(i), i) for i in range(5, 8): test_unit.assertTrue(cus_map.remove(Key(i)) == i) for i in range(1, 4): cus_map.put(Key(i), i + 5) test_unit.assertTrue(cus_map.size() == 21) test_unit.assertTrue(cus_map.get(Key(1)) == 6) test_unit.assertTrue(cus_map.get(Key(2)) == 7) test_unit.assertTrue(cus_map.get(Key(3)) == 8) test_unit.assertTrue(cus_map.get(Key(4)) == 4) test_unit.assertTrue(cus_map.get(Key(5)) == None) test_unit.assertTrue(cus_map.get(Key(6)) == None) test_unit.assertTrue(cus_map.get(Key(7)) == None) test_unit.assertTrue(cus_map.get(Key(8)) == 8) print('***' * 10) visitor = Visitor(visit) cus_map.traversal(visitor)
class HashSet(BaseSet): def __init__(self): self.__hash_map = HashMap() def size(self) -> int: return self.__hash_map.size() def is_empty(self) -> bool: return self.__hash_map.is_empty() def is_contains(self, key) -> bool: return self.__hash_map.contains(key) def add(self, key): self.__hash_map.put(key, None) def remove(self, key): self.__hash_map.remove(key) def traversal(self, visit=None): """ def visit(e): print(e) return True if e == 10 else False :param visit: lambda函数, 类似于上面形式的 :return: """ # assert visit is not None, "visit不能为空,请传入一个lambda函数" return self.__hash_map.traversal(visit)
def test2(map: HashMap, test_unit): p1 = Person('abc', 18, 1.78) p2 = Person('abc', 18, 1.78) map.put(p1, 1) map.put(p2, 2) map.put('abc', 3) map.put('xyz', 4) map.put('abc', 5) map.put(None, 6) visitor = Visitor(visit) map.traversal(visitor) test_unit.assertTrue(map.size() == 4) test_unit.assertTrue(cus_map.get('xyz') == 4) test_unit.assertTrue(cus_map.get(None) == 6) test_unit.assertTrue(map.contains_key('abc')) test_unit.assertTrue(map.contains_value(2)) test_unit.assertTrue(map.remove('abc') == 5) test_unit.assertTrue(map.size() == 3)