Hashmap Java
Why Hashmaps?
Hashmap stores unique value corresponding to a key.
If you try to give value for a key twice or more time then it will override the value.
Code:
import java.util.*;class hashmaps
{
public static void main(String args[])
{
HashMap ab=new HashMap(); //object for hashmap class
ab.put("key1","its me");
ab.put("key2","its me2");
ab.put("key3","its me3"); // adding value to hashmap
ab.put("key4","its me4");
ab.put("key5","its me5");
ab.put("key6","its me6");
System.out.println(ab.get("key1")); // getting value corresponding to key1
System.out.println(ab.get("key2"));
System.out.println(ab.get("key3"));
System.out.println(ab.get("key4"));
System.out.println(ab.get("key5"));
}}
Inner Work :
It produces index (bucket value) by hashing hashcode & size-1 which performs a remainder like operation and gives index where value can be placed.
if more than one key has same index then it will link nodes as a linked list i,e, 1st node of same index will hold address of next node.
While fetching it checks that whether index is having the key for which data is needed if not then it checks for next node and so on until found.
0 Comments:
Post a Comment