哈希表的创建方式及用法

创建哈希表

1.使用数组进行哈希表的创建

String [] hashTable = new String[4];

2.使用hashMap创建哈希表

HasnMap<Integer,String> map = new HashMap<>();

向哈希表中添加元素 Add element

hashTable[1] = "xiaohua";
hashTable[2] = "xialli";
hashTable[3] = "xiao";
hashTable[4] = "an";


map.put(1,"xiaohua");
map.put(2,"xiao");
map.put(3,"xiaoli");
map.put(4,"an");

更新元素

hashTable[1] = "fish";
map.put(1,"fish");

删除元素

hashTable[1] = "";
map.remove(1);

获取元素

String t = hashTable[1];
map.get(1);

检查key是否存在

//时间复杂度 O(1)
map.containsKey(1);

长度

// 时间复杂度
map.size();
map.isEmpty();

你可能感兴趣的:(java)