Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




JAVA - SCRIPT-HASH TABLE CODE




Example :



<html>
<body>    
<script>

    var hashtable = {}

    hashtable["ABA"] = 13
    hashtable["CAB"] = 12
    hashtable["BAC"] = 15

	for (key in hashtable)
    	document.write("The key is : ", key, " and the value is : ", hashtable[key], "</br>")

    i = hashtable["ABA"]

    document.write("ABAs age is ",i, "</br>")

    j = hashtable["BAC"]

    document.write("BACs age is ",j, "</br>")

</script>
</body>
</html>


Output :



  The key is : ABA and the value is : 13
  The key is : CAB and the value is : 12
  The key is : BAC and the value is : 15
  ABAs age is 13
  BACs age is 15

Code explanation for Hash Table


Luckily, JavaScript already provides Object that represents HashTable.


As we can see we have stored the names ABA, CAB and BAC and their corresponding ages 13, 12 and 15 in the Hashtable.


hashtable["ABA"] = 13
hashtable["CAB"] = 12
hashtable["BAC"] = 15

But how they are stored in the HashTable will be decided by JavaScript. As we are using the Object provided by JavaScript.


So, for ABA, a Hash Code is calculated by JavaScript and the age of ABA is stored in some location decided by JavaScript.


Similarly, the age of CAB and BAC is stored in the Hashtable after JavaScript calculates the Hash Code, and decides where they will be stored.


After all the values are stored, we then check all the values stored in the Hashtable.


for (key in hashtable)
	document.write("The key is : ", key, " and the value is : ", hashtable[key], "</br>")

Then we try to retrieve the age of ABA.


i = hashtable["ABA"]

So, we pass the name of ABA and internally JavaScript calculates the Hash Code for ABA and gives us the age of ABA from the Hashtable.


And we print the age of ABA on the screen.


document.write("ABAs age is ",i, "</br>")

Similarly we retrieve the age of BAC and print it on the screen.


j = hashtable["BAC"]

document.write("BACs age is ",j, "</br>")