HashMap overview

Its basically designed to store key value pairs

HashMap class implements the Map interface and it is built using hashtable

Main features of HashMap


It stores data in the form of key-value pair

Keys in the HashMap must be unique

It allows one Null key and multiple Null values

It does not maintain the order of insertion

Value can be retrieved by passing key

If passed key is not exist in the HashMap then value returned will be Null

HashMap can be created using constructor as below

  1. HashMap<Integer,String> map = new HashMap<Integer,String>();
HashMap<Integer,String> map = new HashMap<Integer,String>();


Most Important Methods in Hashmap

put(Object key, Object value)

This method stores the specified value and associates it with the specified key in this map.

get(Object key)

This method will return the value associated with a specified key in hashmap.

We can add key value pair as below

  1. map.put(1,”one”);
map.put(1,”one”);


We can retrieve value for the specific key as

  1. map.get(1);
map.get(1);


Example

  1. import java.util.*;
  2. public class HashMapExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.  
  7.       hashMap.put(1, "One");
  8.       hashMap.put(2, "Two");
  9.       hashMap.put(3, "Three");
  10.       hashMap.put(4, "Four");
  11.       hashMap.put(5, "Five");
  12. System.out.println(hashMap);
  13. System.out.println("Value for the key 1 is "+hashMap.get(1));
  14. System.out.println("Value for the key 10 is "+hashMap.get(10));
  15. }
  16. }
import java.util.*;
public class HashMapExample {
public static void main(String args[]) {

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

      hashMap.put(1, "One");
      hashMap.put(2, "Two");
      hashMap.put(3, "Three");
      hashMap.put(4, "Four");
      hashMap.put(5, "Five");
System.out.println(hashMap);
System.out.println("Value for the key 1 is "+hashMap.get(1));
System.out.println("Value for the key 10 is "+hashMap.get(10));
}
}
Note : We can see that corresponding value for existing key is retrieved and value for non-existing key is returned as Null

When to use HashMap?

Whenever we want to store data in key-value pair format
Whenever we don’t worry about order of insertion

When not to use HashMap ?

Whenever we have collection of elements without any link in that data like key value pair
Whenever we need to maintain the order of insertion

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on