Sponsored Links

Ad by Google
In our previous post we have seen a very very popular java interview question, What is difference between HashMap and Hashtable here and in this post we are going to show you What is HashMap? and How to use HashMap? and also How to iterate HashMap.

Well HashMap is an implementation of java.util.Map interface. HashMap basically allows you to store the element(s) in the form of key and value pair. HashMap provides all of the optional Map operations and allows one null as a key and any number of value as null. Of course you can say it's roughly equivalent to Hashtable except that HashMap is not synchronized and it also allow null as key unlike Hashtable which does not.
HashMap does not maintain the insertion order, so you can say HashMap is un-sorted and un-order Map implementation.
To put element(s) into HashMap it uses put() method and to retrieve back the element(s) from the HashMap, it uses get() method and both of these methods are gives constant time performance.
For performance point of view HashMap has two parametrized constructor with initial capacity and load factor. The initial capacity is basically number of buckets, the default initial capacity is 16. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased, default load factor is .75. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed(rebuilt by swamping the previous values into new hash map). Of course the default load factor offers great tradeoff between time and space, higher value of load factor will decrease the space overhead but increase the searching.
You can change it by providing the value of initial capacity and load factor at the time of creation using below statement.

HashMap<String, String> hMap = new HashMap<>(20,.80f);

HashMap is basically based on hashing principal so the best hash key will gives you best performance while retrieving elements from the HashMap. Very popular known key is String because of it's immutability see Why String is immutable.

How to use HashMap

package com.javamakeuse.poc;

import java.util.HashMap;

public class HashMapExample {
	public static void main(String[] args) {
		HashMap<String, Double> saleMap_2015 = new HashMap<>();
		saleMap_2015.put("Jan", 30000d);
		saleMap_2015.put("Feb", 25000d);
		saleMap_2015.put("March", 31000d);

		// getting elements from the map
		System.out.println("January sale value - " + saleMap_2015.get("Jan"));

How to iterate HashMap

for (String key : saleMap_2015.keySet()) {

 System.out.println("The sale of " + key + " is - "
             + saleMap_2015.get(key));
Numbers of ways to iterate HashMap and those are listed here How Many Ways to iterate HashMap in java here.
HashMap is not a synchronized map but you can make it synchronized by using synchronizedMap method of Collections.


Top 5 Java Interview Questions

Which Collection is better to store 10 millions of elements between ArrayList and LinkedList Answer
Design your own ArrayList Answer
Serializable Vs Externalizable Answer
Java Program to find missing number in an Array Answer
How to create your own Immutable class Answer

Sponsored Links


Post a Comment