Map Interface

public interface Map extends Collection
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
The Map interface provides three collection views, 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. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.
Map intrface is part of collectiion framework but a map is not a collection. So Map interface does not extend the Collection interface. A map does not contain elements. It contains mappings (also called entries) from a set of key objects to a set of value objects.
Map interface features:
  • A map cannot contain duplicate key.
  • Each key can map to at most one value.
  • A map does not contain elements..
  • It contains mappings (also called entries) from a set of key objects to a set of value objects.
  • The SortedMap interface extends the Map interface to maintain its mappings sorted in key order.
Method of Map interface
  1. public int size();
  2. public boolean isEmpty();
  3. public boolean containsKey(Object o);
  4. public boolean containsValue(Object o);
  5. public Object get(Object o);
  6. public Object put(Object o, Object obj);
  7. public Object remove(Object o);
  8. public void putAll(Map map);
  9. public void clear();
  10. public Set keySet();
  11. public Collection values();
  12. public Set entrySet();
  13. public boolean equals(Object o);
  14. public int hashCode();
Entry interface
The Entry interface is nested interface of Map interface. Therfore it is used for access Map.Entry interface with a map entry. The entrySet( ) method declared within Map interface and its returns type a Set containing the map entries. Each of these set elements is a Map.Entry object.
Method of Entry interface
  1. public Object getKey();
  2. public Object getValue();
  3. public Object setValue(Object o);
  4. public boolean equals(Object o);
  5. public int hashCode();
Example of HashMap class
import java.util.HashMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class HashMapDemo { public static void main(String args[]) { HashMap hm = new HashMap(); Set set; Map.Entry m; double bal; hm.put("Sunil",134.34); hm.put("Anand", 1023.22); hm.put("Sanjeev",15678.00); hm.put("Sujeet", 5647.22); // Show all set of hash Map. set = hm.entrySet(); // get iterator Iterator it=set.iterator(); while(it.hasNext()) { m = (Map.Entry); System.out.println(m.getKey() + ": " + m.getValue()); } System.out.println(); // Add 1350.67 into Sunil value bal = ((Double)hm.get("Sunil")).doubleValue(); hm.put("Sunil", new Double(bal+1350.67)); System.out.println("Sunil new Value: " + hm.get("Sunil")); } }

Sujeet: 5647.22
Sunil: 134.34
Sanjeev: 15678.0
Anand: 1023.22

Sunil new Value: 1485.01

Download this example


Latest Tutorial

Most Visited Tutorial