3.24 快速查找、缓存、计数--哈希表
- 创建哈希表
java
import java.util.HashMap;
HashMap<KeyType, ValueType> hashMap = new HashMap<>();
2. 添加键值对
java
hashMap.put(key, value);
3. 获取值
java
ValueType value = hashMap.get(key);
4. 检查键是否存在
java
boolean exists = hashMap.containsKey(key);
5. 检查值是否存在
java
boolean exists = hashMap.containsValue(value);
6. 删除键值对
java
hashMap.remove(key);
7. 获取哈希表的大小
java
int size = hashMap.size();
8. 清空哈希表
java
hashMap.clear();
9. 遍历哈希表
使用for循环遍历键值对
java
for (Map.Entry<KeyType, ValueType> entry : hashMap.entrySet()) {
KeyType key = entry.getKey();
ValueType value = entry.getValue();
// 处理键值对
}
wordCount.put(word, wordCount.get(word) + 1);这行代码什么意思 get可以获取当前的值

浙公网安备 33010602011771号