HashMap Class in Java
Java HashMap class is a Hash Table implementation of Map interface. It stores data in key-value pairs. Unlike an ArrayList, HashMap class doesn't use indices to sort values.
What is HashMap in Java?
Java's HashMap class is a Hash Table implementation of Map interface. It stores data in key-value pairs.
When to use HashMap in Java?
Unlike an ArrayList, HashMap implementations don't use indices to sort values. Therefore to access each entry, one must know the key of the entry/object.
So if you have several key-value pairs or data that can be identified using keys, then Java's HashMap class is for you.
More about HashMap
The keys used to store data must be unique, which means multiple entries with same key is not possible in HashMap. Also, there's no order to those entries.
The HashMap class extends AbstractMap class and implements the Map interface in Java.
Output
Enter ID: npl Enter Password for npl nplpass Enter ID: pkr Enter Password for pkr pkrpass Enter ID: ktm Enter Password for ktm ktmpass Enter ID: btl Enter Password for btl btlpass Enter ID: bkt Enter Password for bkt bktpass Enter ID: llt Enter Password for llt lltpass Enter ID to query the Password: pkr The password for ID: pkr is: pkrpass