HashMap search
In this chapter you will learn:
Check key existence
containsKey(Object key)
checks to see if the HashMap has that key
import java.util.HashMap;
/*from j a v a2 s .c o m*/
public class Main {
public static void main(String[] args) {
HashMap<String, String> hMap = new HashMap<String, String>();
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Three");
boolean blnExists = hMap.containsKey("3");
System.out.println("3 exists in HashMap ? : " + blnExists);
}
}
Value existence
containsValue(Object value) checks if the HashMap has that valueimport java.util.HashMap;
import java.util.Map;
/* jav a2s . co m*/
public class Main {
public static void main(String[] a) {
Map<String,String> map = new HashMap<String,String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println(map.containsKey("key1"));
System.out.println(map.containsValue("value2"));
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Map