Advertisement



< Prev
Next >



HashMap Class





HashMap class extends AbstractMap and it implements Map interface. HashMap stores the data in the form of key-value pairs, where key and value are objects. Each key-value pair may also referred to as a map-entry.




Use of HashMap







How values are stored in a HashMap?







A simple constructor of HashMap


HashMap()
This constructor creates an empty HashMap to hold a particular type of key-value pair objects.
Example -
HashMap <String,Integer> hm = new HashMap <String,Integer>();

This constructor example creates a HashMap to hold key-value pairs of String and Integer objects.




Some methods in a HashMap.


Methods Description
void clear() Removes all the key/value pairs from the HashMap.
int size() Returns the total number of key-value pairs in a HashMap.
boolean isEmpty() Returns true if HashMap has no key-value pair in it.
V get(Object key) Returns the value associated with a specified key.
put(K key, V value) Puts the specified key and its associated value in the HashMap.
boolean containsKey(Object key) Returns true if HashMap contains a particular key, otherwise false.
Set<Map.Entry<K,V>> entrySet() Returns the Set containing Map.Entry objects.





Creating a HashMap


In this example we will create a HashMap to store object in the pair of key and value, where key is a String object and value is an Integer object. As you know that given a key, we can find its value, hence, we will obtain a value on the basis of a particular key.

import java.util.*;

class HashMapDemo
{
public static void main(String... ar)
{
HashMap <String,Integer> hm = new HashMap <String,Integer>();

hm.put("Max", 1000);
hm.put("John", 4000);
hm.put("Tom", 2000);
hm.put("Ana", 6000);
hm.put("Rick", 5000);

System.out.println(hm);

System.out.println("Value at the key, Tom is "+ hm.get("Tom"));
System.out.println("Value at the key, Ana is "+ hm.get("Ana"));

}

}


Output is :


{Max=1000, Tom=2000, John=4000, Rick=5000, Ana=6000}
Value at the key Tom is 2000
Value at the key Ana is 6000


Program Analysis





Advertisement




Iterating over each key-value pair in a HashMap using entrySet() method


We will create a HashMap to store objects in the pair of key and value, where key is a String object and value is an Integer object. We will use entrySet() method to get a Set view of this HashMap. We will iterate over each map entry(key-value pair) using for-each loop.

import java.util.*;

class HashMapDemo
{
public static void main(String... ar)
{
HashMap <String,Integer> hm = new HashMap <String,Integer>();

hm.put("Max", 1000);
hm.put("John", 4000);
hm.put("Tom", 2000);
hm.put("Ana", 6000);
hm.put("Rick", 5000);

System.out.println("Iterating HashMap using Map.Entry in a for-each loop");
Set<Map.Entry<String,Integer>> set = hm.entrySet();


for(Map.Entry<String,Integer> mapE : set)
{
	System.out.print(mapE.getKey() + " : ");
	System.out.println(mapE.getValue());
}

}
}


Output is :


Iterating HashMap using Map.Entry in a for-each loop
Max : 1000
Tom : 2000
John : 4000
Rick : 5000
Ana : 6000


Program Analysis






Please share this article -




< Prev
Next >
< Maps
TreeMap >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement