Java Map

Java Map Example

Java Map interface is the one that stores values on the basis of keys and their values where each map has unique keys.  An entry in a map consists of a key and a value.  The map interface is present in java.util.Map package in java which maps both the value and the key. This interface has a little different behavior from the other collection fields as this has only unique values. Map in java is not a class, it is an interface. Since the map is an interface, it can only implement interfaces and not the classes. The user can perform a number of operations in the map interface.

Hence a map contains 3 sets:

  • Keys
  • Values
  • Map to associate keys and values together

Features of Java Map:

  • Map interface in java cannot have duplicate key values. Each key can map a single value only.
  • A map might or might not allow null values. This depends on the type of collection.
  • There are three sub-interfaces of maps in java. They are HashMap, TreeMap and LinkedHashMap.
  • The program throws an exception ClassCastException if the elements are not compatible with the map.
  • When the null is not allowed in the map and a null object is requested to use, an exception is thrown NullPointerException.
  • An exception UnsupportedOperationException is thrown in case the user tries to change a map that can’t be modified.

Difference between HashMap and hashtable:

In Java HashMap vs Hashtable, Here a hashmap is not properly synchronized whereas a hashtable is a synchronized form of collection. One major difference between hashmap and hashtable is it can be shared among multiple threads, whereas we cannot do the same for hashmap. It cannot be shared between multiple threads without a proper code of synchronization. A hashtable can never be unsynchronised. In cases where thread synchronization is not needed, a hashmap is always preferred over hashtable in the collection.

Hashmap allows multiple null key values in it whereas a hashtable never allows null values in it. Also, hashmap tends to inherit an Abstract class whereas a hashtable tends to inherit a dictionary. Hashmap is basically an advanced version of the hashtable.

There’s a sub category of hashmap which is LinkedHashMap that maintains the order of the elements to be added. However, in case of hashtable there is nothing like an order. A hashmap maintains no kind of order of elements or of the mapping.

Fail fast is a system in java which tends to fail immediately when an error occurs. This usually happens while iterating through the problem. HashMap is a fail-fast in java whereas a hashtable is not. Hashmap throws a ConcurrentModificationException in case an error is about to occur. This problem only occurs because an iterator works in the case of HashMap. An enumerator however doesn’t let a hashtable fail-fast

Take a look below to understand the operations on the map:

  • Add Elements: the user can use put() method in order to add elements to the map. Each element should have a separate hashmap for each element. Hence, it is generated and each element is indexed on the map. This makes the insertion of the elements very efficient. There’s another operation putAll() which adds all the elements in the map.
  • Change Elements: The user might wish to change the elements after adding them by using the above process. The user can also do it by by adding the new element using put() method. Since, map has keys to index the numbers, the key can simply be changed according to the new value that user wishes to change.
  • Deleting Element: If a user wants to remove the element, he can use remove() method in java maps. This method will remove the mapping from the key by taking the key value. It checks first if the key is present in the map or not. Then if it is present, it will remove the value.
  • Iteration through map: The best way to do this process in the map interface is by the use of for loop. Though, there are a number of ways for iterating through the map, but the best way is by using for loop. The user can get the value of the by using getValue() method in java.

How to Create a map program in java

import java.util.*; 
public class DeveloperHelps { 
public static void main(String[] args) { 
Map<String, Integer> map = new HashMap<>(); 
map.put("abc", 100); 
map.put("xyz", 101); 
map.put("uvw", 102); 
for (Map.Entry<String, Integer> e : map.entrySet()) 
System.out.println(e.getKey() + " " + e.getValue()); 

The output of this java program will be:

abc 100
uvw 102
xyz 101

Leave a comment

Your email address will not be published. Required fields are marked *