Advertisement



< Prev
Next >



Map Interface





Map is an interface and though its implementation classes, we can create an object which is used to store data in the form of key-value pairs, where key and value are objects. Each key can be used to extract a value linked with it i.e. if you know the key, you can retrieve the value with it. Each key-value pair may also referred to as a map entry. A Map object is a type of class which has implemented Map interface.




Some important classes that implement Map interface are -:


Classes Description
AbstractMap An abstract class but still provides most of the implementation of Map interface, to minimize the effort required by other classes to implement it.
HashMap This class extends AbstractMap and uses Hash table to store the key-value pairs/map entries. Ordering of its elements may appear out-of-order.
TreeMap This class extends AbstractMap and it sorts its elements according to the natural ordering of its keys.
LinkedHashMap This class extends AbstractMap and it uses the insertion order of it's elements to provide a consistent ordering.





Some important points about Map




A generic Map is declared as

interface Map<K,V>

where K stands for type of keys and V stands for types of values we are going to store in a Map.





Some method of Map interface.


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.



Advertisement




How values are stored in a Map?


We put values in a Map by passing a key-value pair to put() method of Map.




Map.Entry interface


We read at the beginning of this article that method entrySet() of Map interface returns a Set(collection class) view of a Map. This Set contains objecs of type Map.Entry, to call some highly userful methods of Map.Entry interface in order to iterate over each element(key-value pair) of a Map.




A few very important methods of Map.Entry interface -:


Methods Description
K getKey() This method returns the key of the current map entry or current key-value pair.
V getValue() This method returns the value of a current map entry or current key-value pair.
int hashCode() This method returns the hashcode of current key-value pair.





Understanding the working of Map's interface methods -:




import java.util.*;

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

hm.put("Max", 1);
hm.put("John", 4);
hm.put("Tom", 2);
hm.put("Ana", 6);
hm.put("Rick", 5);

System.out.println("Iterating TreeMap using Map.Entry and for-each loop");
Set<Map.Entry<String,Integer>> set = tm.entrySet();

//for-each loop to iterate each element of Map, using Map.Entry objects and methods
for(Map.Entry <String,Integer> me : set)
System.out.print(getKey() +":");
System.out.println(getValue());

}

}


Output is :


Iterating HashMap using Map.Entry in a for-each loop
Max:1
Tom:2
John:4
Ana:6
Rick:5


Program Analysis




We will see read much more about these concrete subclasses of Map interface in the next few articles. Stay connected.



Please share this article -





< Prev
Next >
< Iterator v/s ListIterator
HashMap >



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 and DJANGO


Advertisement