使用该containsKey()方法检查IdentityHashMap中是否存在特定键。
创建一个IdentityHashMap
Map<String, Integer> m = newIdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);现在,假设我们需要检查键7是否存在。为此,请使用如下containsKey()方法
m.containsKey(“7”)
以下是检查IdentityHashMap中是否存在键的示例
import java.util.*;
public class Demo {
public static void main(String[] args) {
Map<String, Integer> m = newIdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);
System.out.println("IdentityHashMap elements\n"+ m);
System.out.println("Size = " + m.size());
System.out.println("Is key 7 in the Map? " + m.containsKey("7"));
}
}输出结果
输出如下
IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10
Is key 7 in the Map? True