Java collections Framework- Map Interface

Java collections Framework- Map Interface


Posted in : Java Posted on : December 2, 2010 at 5:21 PM Comments : [ 0 ]

A Map is an object that maps keys to values. It is not an extension of the collection interface rather it has own interface hierarchy.

Java collections Framework- Map Interface

A Map is an object that maps keys to values. It is not an extension of the collection interface rather it has own interface hierarchy. Map provides a more general way for storing elements without containing duplicate keys. It allows you to store pairs of elements, termed "keys" and "values", where each key maps to one value. Thus the keys in a map must be unique.

The Map interface methods can be broken down into three sets of operations:

Altering: The alteration operations allow you to add and remove key-value pairs from the map. Both the key and value can be null.
Querying:
The query operations allow the user to retrieve key/value pairs from the Map.
Providing alternative views:
This method allow you to work with the different views which can be used to analyze Map key/value Objects.

Map Methods Returning Views:

These methods return objects which allow you to traverse the elements of the Map, and also delete elements from the Map.

entrySet() - Returns a Set view of the mappings contained in the map. Each element in the set is a Map.Entry object which can have it's key and value elements accessed with getKey() and getValue() methods (also has a setValue() method)

keySet() - Returns a Set view of the keys contained in the map. Removing elements from the Set will also remove the corresponding mapping (key and value) from the Map

values() - Returns a Collection view of the values contained in the map. Removing elements from the Collection will also remove the corresponding mapping (key and value) from the Map

Map.Entry Interface:

Each element is a map has a key and value. Each key-value pair is saved in a java.util.Map.Entry object. A set of these map entries can be obtained by calling a map's entrySet( ) method. Iterating over a map is done by iterating over this set.

The Collections Framework provides three general-purpose Map implementation:

1 - HashMap
2 - TreeMap
3 - LinkedHashMap

HashMap:
The HashMap is a class which is used to perform some basic operations such as inserting, deleting, and locating elements in a Map . The  java.util.HashMap class is implemented with and roughly equivalent to a Hashtable  except that it is unsynchronized and permits null. It works with the Iterators requires a well-defined implementation of the method hashCode( ).

TreeMap:
The TreeMap implementation is useful when you need to traverse the keys from a collection in a sorted manner. The elements added to a TreeMap must be sortable in order to work properly. It is depend upon the size of the specified collection, it may be faster to add elements to a HashMap , then convert the map to a TreeMap for traversing the sorted keys.

LinkedHashMap:
A LinkedHashMap is implemented using both Hash table and linked list implementation of the Map interface. This implementation differs from HashMap that maintains a doubly-linked list running through all of its entries in it. The orders of its elements are based on the order in which they were inserted into the set (insertion-order).

The list of methods supported by Map interface are given below :      

  • clear( ) - This method deletes the all mappings from the map
  • containsKey(Object key) - This method checks for the specified key whether the underlying map contains.
  • containsValue(Object value) - This method checks whether the underlying map mappings a specific value with at least a single or more keys.
  • entrySet( ) -  This method gives back a view of set of the mappings contained into the underlying map.
  • equals(Object o) - This method processes an equivalence comparison for a specific object within the underlying  map.
  • get(Object key) - This method gives back a 'value' of the specified key if the underlying map contains mapping for that key otherwise returns 'null'.
  • hashCode() -  This method gives a hash code value for the underlying map.
  • isEmpty( ) - This method check whether the underlying map has the 'key-value' mappings, or not.
  • keySet( ) - This method gives back a view of set of the 'keys' kept by this map.
  • put( ) - This method associates a specific value with a particular key in the map.
  • putAll(Map m ) - This method transcripts all the 'key-value' association from a specific map to the underlying map.
  • remove( ) -  This method deletes the association for a key from the underlying map if it is present.
  • size( ) -  This method gives back the total number of 'key-value' associations of the underlying map.
  • values( ) - This method gives back a view of collection of the values contains into this map. 

Here is the example for demonstration of the map interface-

Example:- MapDemo.java

package devmanuals.com;

import java.util.*;

public class MapDemo {
	public static void main(String[] args) {

		Map mp = new HashMap();

		mp.put(2, "Two");
		mp.put(1, "One");
		mp.put(3, "Three");
		mp.put(4, "Four");
		System.out.println(mp);

	}
}
Output:-
{1=One, 2=Two, 3=Three, 4=Four}

Download This Code.
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics