共計 456 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,containsKey() 是用于判斷一個 Map 集合中是否包含指定的鍵的方法。它返回一個 boolean 類型的值,如果存在指定的鍵,則返回 true;否則返回 false。
使用示例:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
boolean containsKey = map.containsKey("apple");
System.out.println(containsKey); // 輸出 true
containsKey = map.containsKey("grape");
System.out.println(containsKey); // 輸出 false
在上述示例中,首先創建了一個 Map 對象,并向其添加了三個鍵值對。然后使用 containsKey() 方法分別判斷是否存在指定的鍵 "apple" 和 "grape",并將結果輸出。
丸趣 TV 網 – 提供最優質的資源集合!
正文完