Core Java - SPLessons

Java Hashmap

Chapter 42

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Hashmap

Java Hashmap

shape Description

Java Hashmap, HashMap maintains key and value pairs and often denoted as HashMap or HashMap. HashMap implements Map interface . Java Hashmap is similar to Hashtable with two exceptions – HashMap methods are unsynchornized and it allows null key and null values unlike Hashtable. It is used for maintaining key and value mapping. It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap.

Constructors and Methods

shape Description

Java Hashmap, Following are the methods and constructor of Hashmap class.
Constructors Description
HashMap( ) To construct default HashMap.
HashMap(int capacity) To initialize the capacity of the hash map to the given integer.
HashMap(Map m) To initialize the hash map by utilizing the elements of object m.
Methods Description
Collection values() To return the collection view of the values
Set keySet() To Return a set view of the keys.
void clear() To vanish the all mappings.
Object clone() To return a copy of HashMap instance.

shape Example

Following is an example for the Java Hashmap.
[java]class HashMap{
public static void main(String args[]){

HashMap hm=new HashMap();

hm.put(11,”kL Rahul”);

for(Map.Entry m:hm.entrySet())
System.out.println(m.getKey()+” “+m.getValue());
} [/java]
The entrySet( ) method declared by the Map interface returns a Set containing the map entries.

When compile the code result will be as follows.
10 Sam
11 kL Rahul
12 Samson

LinkedHashMap Class

shape Description

Java Hashmap, LinkedHashMap class extends HashMap, deals with the connected rundown of the sections in the map, while repeating a LinkedHashMap, every one of the components will be returned in the request in which components were embedded. Following is an example.
[java]package socket;

import java.util.*;

public class LinkedHashMapDemo {

public static void main(String args[]) {
// Create a hash map
LinkedHashMap lhm = new LinkedHashMap();
// Put elements to the map
lhm.put(“Sam”, new Double(2434.34));
lhm.put(“Sachin”, new Double(1234.22));
lhm.put(“Lara”, new Double(178.00));
lhm.put(“David”, new Double(999.22));
lhm.put(“Holder”, new Double(-199.08));

// Get a set of the entries
Set set = lhm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry);
System.out.print(me.getKey() + “: “);
// Deposit 1000 into Sam’s account
double balance = ((Double)lhm.get(“Sam”)).doubleValue();
lhm.put(“Sam”, new Double(balance + 1000));
System.out.println(“Sam’s new balance: ” +
Here first create the hash map.
[java]LinkedHashMap lhm = new LinkedHashMap();[/java]
Put all the elements into the hashmap as follows.
[java] lhm.put(“Sam”, new Double(2434.34));[/java]
Get a set of the entries as follows.
[java] Set set = lhm.entrySet();[/java]

When compile the code result will be as follows.
[java]Sachin: 1234.22
Lara: 178.0
David: 999.22
Holder: -199.08

Sam’s new balance: 3434.34

Difference between hashmap and hashtable

Hashtable and HashMap are utilized to store information in key and value form. Both will use hashing technique to store unique keys. The following are the some main difference.
  • HashMap is non synchronized where as Hashtable is synchronized.
  • Hahtable is a legacy class where as hashmap is new.
  • HashMap uses iterator where as HashTable uses enumerator and iterator.


shape Key Points

  • HashMap class maintains no order.
  • HashMap can extend the AbstractMap.
  • void clear() methods removes all the mappings from the map.