In the collection framework Map interface is also the important core interface. So now we are going to discuss the Map interface.

The Map interface:-

  • Maps are sometimes called as associative arrays.
  • The object of a Map interface describes mapping from key to value.
  • By the definition, Map interface does not allow the duplicate elements and a key can map to one value at most.
  • A Map interface does not extend a collection interface because Map interface represents mapping not collection of an object.
  • The sorted map interface extends the Map interface.
  • Some of the classes which implement a Map interface are HashMap, TreeMap, IdentityHashMap and WeakHashMap.
  • The order presented by an iterators of these map collection implementations is specific to each iterator.

Map interface provides three methods .viz:-

1)    entrySet:-

this method returns Set and it contains all the key value pair.

2)    keySet:-

This method also returns Set of all the keys in the map.

3)    values:-

This method returns a collection which containing all the values contained in the map.

collections in JAVA

A Map interface:-

  1. import java.util.*;
  2. public class MapDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. Map map = new HashMap();
  7. map.put(“one”,”1st”);
  8. map.put(“second”,new Integer(2));
  9. map.put(“third”,”3rd”);
  10. // overwrites the previous assignment
  11. map.put(“third”,”III”);
  12. // returns the set view of keys
  13. Set set1 = map.keySet();
  14. // returns the Collection view of keys
  15. Collection collection = map.values();
  16. // returns the set view of key value mapping
  17. Set set2 = map.entrySet();
  18. System.out.println(set1 + ”\n” + collection + ”\n” + set2);
  19. }
  20. }

Output:-

[second, one ,third]

[2, 1st, III]

[second = 2, one = 1st, third = III]

  • In above example, map variable is declared of type Map which is assigned to the new HashMap Object.
  • After that by using put operation few elements are added. To prove that duplicate keys are not allowed in Map interface, program attempts to add a new value using an existing key.
  • This results in the previously added value for the key which is being replaced by a new value.
  • The program later uses the collection view operations keySet, values and entrySet fro retrieving the contents of the map.

Legacy Collection Classes:-

The collection classes in the JDK version 1.0 and JDK version 1.1 still exist in the current JDK with same interface, but they have been retooled to interact with the new Collection API.

  • The Vector class which implements the List interface.
  • The Stack class which is an extension of the Vector that adds the typical stack operations: push, poop and peek.
  • The Hashtable which is an implementation of Map interface.
  • Properties class is an extension of the Hashtable that only uses String for keys and values.
  • Each of these collections has an element method that returns an Enumeration object. The Enumeration is an interface similar to but incompatible with the Iterator interface. For example, hasNext is replaced by hasMoreElements in the Enumeration interface.

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment