Search in HashMap

In the tutorial, we will discuss, How can you search in HashMap or how can you search the particular value or key in HashMap Class.

 

Search for Key in HashMap

In this example, we are gonna see how to check if a particular key is present in HashMap. We will be using the containsKey() method of HashMap class to perform this check.

Output:

Key 5 is present in the HashMap : true
Key 50 is present in the HashMap : false
Key 34 is present in the HashMap : true

 

In the above code, you can see containsKey() method is returning true if the key is present and false if the key is not present in the HashMap. Like this, we can verify that key is present in the HashMap or not.

 

Search value in HashMap

In this example, we are checking whether a particular value exists in HashMap or not. We will be using the containsValue() method of HashMap class to perform this check. The method will return true if the value is present else false.

Output:

Mirilla is present in the HashMap : true
Selenium is present in the HashMap : false
Matt is present in the HashMap : true

 

You can see, containsValue() method is returning true if value is present in the map else it returning false. Like this, we can search the value in the HashMap.

 

If you need to find the value at particular index then you can find this using hashmap object and get() method. Suppose you want to find the value at index 4 then your code will be –

hmap.get(4);

This will return the value present at index 4.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply