What is the use of hashmap and hashtable in selenium webdriver?.

Answered

Hi Guys,

I do not have knowledge on “hashmap and hashtable” methods in java, but wanted to know       1. how to use these in selenium webdriver 2. Advantages of these?.

Could you please explain me with the code samples.

Doctor Asked on August 24, 2014 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)
    Best answer

    Hi Rocky,

    Thanks for asking the question. On the outset I would like to tell you that your question is not related to Selenium. Hashtable is data structure that is used to store objects.

    Before we go around explaining a hash table lets understand how elements in array are stored. Consider an array of names like this
    Array names = {“ao”, “ab”, “ac”}. Elements are stored at index values. Index 0 here stores “ao” and index 1 stores “ab”. Arrays have one problem, if you want to search for a name “ac” you have to iterate through the element of array till you reach the desired element. In this case at max you have to iterate 3 times to reach the desired element. This makes the searching of values to complexity O(n) where n is the number of elements in the array. To overcome this linear time complexity of arrays computer scientists came up with the concept of hash tables.

    Hashing function: Hashing function is a piece of logic which converts a datatype  into integer value. This integer value is used as the index to store the item in array.
    Hashing functions are based on variety of algorithms. Lets consider a simple one fr sake of simplicity. We have a hashing function which creates numberical values out of name. It does this by adding up the position of characters in the name . For e.g
    “aa” becomes 1 + 1 = 1, a’s position is 1
    “ac” becomes 1 + 3 = 4, b’s position is 2

    Hash table: Lets say we have an array capable of storing 10  elements.  Now we will store elements in the array based on the hash value

    “aa” ‘s hash value = 2, so we will store “ao” at index 1 of array
    “af” ‘s hash value = 7, so we will store “ab” at index 7
    “ac” ‘s hash value = 4 we will store it at index 4

    Array will look like this Name = { “”. “”, “aa” ,””, “ac”,””,”af”, “”, “”, “”};

    You will see most of the entries are empty in the array. These types of arrays which store values based on the Hash of the datatype are called hash tables. Primary benefit of the hash table is that to retrieve a value all we have to do is just calculate the has and we will know the index of the value and can retrieve it in constant time. This way we get a constant time complexity of data retrievals O(1)

    Hash Map is a class level implementation of hash table.

    Answering your questions
    – Use these data structure when you really need them, not just for the sake of having it in your code.
    – Selenium has no relation with HasMaps. Its really a data structure choice instead of a test framework choice.

    You dont need to code sample to understand this. However, if you want to read more about the java class go to  http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

    A very beautiful explanation about hash table is given here http://www.sparknotes.com/cs/searching/hashtables/section1.html

    Thanks
    Virender

    Professor Answered on August 25, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.