使用该containsKey()
方法检查LinkedHashMap中是否存在特定键。
创建一个LinkedHashMap-
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus");
现在,我们需要检查键4是否存在。为此,请使用如下containsKey()
方法-
l.containsKey(4)
以下是检查LinkedHashMap中是否存在特定键的示例-
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus"); System.out.println("LinkedHashMap elements..."); System.out.println(l); System.out.println("\nDoes key 4 exist in the LinkedHashMap elements? "+l.containsKey(4)); } }
输出结果
LinkedHashMap elements... {1=Mars, 2=Earth, 3=Jupiter, 4=Saturn, 5=Venus} Does key 4 exist in the LinkedHashMap elements? True