Thursday, 20 June 2013

Map Interface in JAVA

A Map is an object that maps keys to values.Each key can map to at most one value. Map interface starts of it’s own interface hierarchy, for maintaining key-value associations. This interface describes a mapping from keys to values, without duplicate keys, by definition.

The Map interface provides three collection views(HashMapTreeMap, and LinkedHashMap), which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements.

Given a key and a value, we can store the value in a Map object. After the value is stored, you can retrieve it by using its key.

Following are list of exception we may get while implementing Map Interface:
  • throw a NoSuchElementException when no items exist in the invoking map.
  • throw a ClassCastException when an object is incompatible with the elements in a map.
  • throw a NullPointerException if an attempt is made to use a null object and null is not allowed in the map.
  • throw An UnsupportedOperationException when an attempt is made to change an unmodifiable map.



Following is the Map Interface:


public interface Map<K,V> {

    // Basic operations
    V put(K key, V value);
    V get(Object key);
    V remove(Object key);
    boolean containsKey(Object key);
    boolean containsValue(Object value);
    int size();
    boolean isEmpty();

    // Bulk operations
    void putAll(Map<? extends K, ? extends V> m);
    void clear();

    // Collection Views
    public Set<K> keySet();
    public Collection<V> values();
    public Set<Map.Entry<K,V>> entrySet();

    // Interface for entrySet elements
    public interface Entry {
        K getKey();
        V getValue();
        V setValue(V value);
    }
}

Following table shows the list of methods available in Map interface:

Example:

package com.sample.javase.testing;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class MapInterfaceExample {

/**
* @param args
*/
public static void main(String[] args) {

Map<String, String> map = new TreeMap<>();
map.put("framework1", "struts");
map.put("framework2", "hibernate");
map.put("framework3", "spring");
System.out.println(map);
/**
* Following lines of code specifies hoe to get the map values form
* iterator. For this first we need to get entry set or key set from
* map.Here we are using entryset() method.
*/
Set set = map.entrySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Map.Entry<String, String> object = (Entry<String, String>) iterator
.next();
System.out.println("key is " + object.getKey());
System.out.println("value is " + object.getValue());
}
/**
* Following lines of code specifies hoe to get the map values form
* iterator. For this first we need to get entry set or key set from
* map.Here we are using keyset() method.
*/
Set set1 = map.keySet();
for (Iterator iterator = set1.iterator(); iterator.hasNext();) {
Object object = iterator.next();
System.out.println("key is " + object);
System.out.println("value is " + map.get(object));
}

}

}

No comments:

Post a Comment