HashMap 的 ContainKey()用法

本文介绍了一种使用Java中的HashMap来存储和管理不同按钮的自定义键的方法,并演示了如何检查一个特定的字符串是否作为键存在于HashMap中。
String description = "你所要比较的字符串";
HashMap hashmap = new HashMap();
hashmap.put(
"prepare receive button custom key""");
hashmap.put(
"cash_pay_all button custom key for check out""");
hashmap.put(
"confirm receive button custom key""");
if (hashmap.containsKey(description)) {
//如果包含,进行相关操作
}
Java 中 HashMap 是一个非常常用的数据结构,用于存储键值对,提供了快速的查找、插入和删除操作。以下是一些常见的使用方法: ### 1. 创建 HashMap ```java import java.util.HashMap; public class Main { public static void main(String[] args) { // 创建一个 HashMap,键和值的类型都是 String HashMap<String, String> hashMap = new HashMap<>(); } } ``` ### 2. 添加键值对 可以使用 `put()` 方法将键值对添加到 HashMap 中,使用 `putAll()` 方法将另一个 Map 中的所有键值对添加到当前 HashMap 中,使用 `putIfAbsent()` 方法在键不存在时插入键值对 [^2]。 ```java hashMap.put("key1", "value1"); hashMap.put("key2", "value2"); HashMap<String, String> anotherMap = new HashMap<>(); anotherMap.put("key3", "value3"); hashMap.putAll(anotherMap); hashMap.putIfAbsent("key4", "value4"); ``` ### 3. 获取值 使用 `get()` 方法根据键获取对应的值,使用 `getOrDefault()` 方法在找不到键时返回设置的默认值 [^2]。 ```java String value = hashMap.get("key1"); String defaultValue = hashMap.getOrDefault("key5", "default"); ``` ### 4. 删除键值对 使用 `remove()` 方法删除指定键的映射关系 [^2]。 ```java hashMap.remove("key2"); ``` ### 5. 检查键或值是否存在 使用 `containsKey()` 方法检查 HashMap 中是否存在指定的键,使用 `containsValue()` 方法检查是否存在指定的值 [^2]。 ```java boolean hasKey = hashMap.containsKey("key1"); boolean hasValue = hashMap.containsValue("value1"); ``` ### 6. 替换值 使用 `replace()` 方法替换指定键对应的值,使用 `replaceAll()` 方法将所有映射关系替换成给定函数执行的结果 [^2]。 ```java hashMap.replace("key1", "newValue1"); hashMap.replaceAll((key, oldValue) -> oldValue + "_updated"); ``` ### 7. 遍历 HashMap 可以通过 `keySet()`、`values()`、`entrySet()` 等方法遍历 HashMap [^3][^5]。 ```java // 遍历键 for (String key : hashMap.keySet()) { System.out.println("键: " + key); } // 遍历值 for (String value : hashMap.values()) { System.out.println("值: " + value); } // 遍历键值对 for (java.util.Map.Entry<String, String> entry : hashMap.entrySet()) { System.out.println("键: " + entry.getKey() + ", 值: " + entry.getValue()); } // 通过迭代器遍历键值对 java.util.Set<java.util.Map.Entry<String, String>> entrySet = hashMap.entrySet(); java.util.Iterator<java.util.Map.Entry<String, String>> iterator = entrySet.iterator(); while (iterator.hasNext()) { java.util.Map.Entry<String, String> entry = iterator.next(); System.out.println("键: " + entry.getKey() + ", 值: " + entry.getValue()); } ``` ### 8. 其他操作 使用 `clear()` 方法删除所有键值对,使用 `clone()` 方法复制一份 HashMap,使用 `isEmpty()` 方法判断 HashMap 是否为空,使用 `size()` 方法计算键值对的数量 [^2]。 ```java hashMap.clear(); HashMap<String, String> clonedMap = (HashMap<String, String>) hashMap.clone(); boolean isEmpty = hashMap.isEmpty(); int size = hashMap.size(); ``` ### 9. 合并和计算操作 使用 `merge()` 方法添加键值对,使用 `compute()`、`computeIfAbsent()`、`computeIfPresent()` 方法对指定键的值进行重新计算 [^2]。 ```java hashMap.merge("key1", "newValue", (oldValue, newValue) -> oldValue + newValue); hashMap.compute("key1", (key, oldValue) -> oldValue + "_computed"); hashMap.computeIfAbsent("key5", key -> "newlyAddedValue"); hashMap.computeIfPresent("key1", (key, oldValue) -> oldValue + "_updatedIfPresent"); ``` ### 10. 对每个映射执行操作 使用 `forEach()` 方法对 HashMap 中的每个映射执行指定的操作 [^2]。 ```java hashMap.forEach((key, value) -> System.out.println("键: " + key + ", 值: " + value)); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值