Java Map Interface, Implementations and Example
A Map interface is an object that maps keys to values or handles key-value pairs. It cannot contain duplicate keys and each key can map to at most one value.
Map interface hierarchy
The Map interface Java SE
The methods of “Map” interface for basic operations, bulk operations and collection views are listed below for your reference.
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); }
“SortedMap” interface
A “Map” interface that maintains its mappings in ascending key order is called the SortedMap interface. This is the Map analog of SortedSet. It extends “Map” interface.
public interface SortedMap<K,V> extends Map<K,V>
Sorted maps are used for naturally ordered collections of key or value pairs, such as dictionaries and telephone directories.
“NavigableMap” interface
NavigableMap interface is an extension of SortedMap interface which provides popular navigation methods like lowerEntry, floorEntry, ceilingEntry, higherEntry etc., that returns the closest matching results for the given search targets. It extends SortedMap interface
public interface NavigableMap<K,V> extends SortedMap<K,V>
Implementations of “Map” interface
The three general-purpose implementations of the Map interface are:
- HashMap: Hash table based implementation of the “Map” interface. It permits null values and the null key. Use this class if you want maximum speed and do not want to care about iteration order. It is most commonly used implementation.
- TreeMap: A Red-Black tree based NavigableMap implementation. TreeMap is sorted according to the natural ordering of its keys. Use this class when you need SortedMap operations or key-ordered Collection-view iteration.
- LinkedHashMap: Hash table and linked list implementation of the Map interface. Although it is somewhat slower than HashMap for adding and removing elements, you can expect faster iteration with a LinkedHashMap.
Example: “Map” Interface
package com.sneppets.java.collections; import java.util.HashMap; import java.util.Map; /** * Program to demonstrate Map Interface Methods * @author sneppets.com */ public class MapInterfaceExample { public static void main (String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); //basic operations System.out.println("Inserting elemens in map..."); map.put(1, "AB"); map.put(2, "BC"); map.put(3, "CD"); map.put(4, "DE"); System.out.println("Map Elements: " + map); //Collection views System.out.println("Traversing through map elements and print key value pairs..."); for (Map.Entry<Integer,String> m: map.entrySet()) { System.out.println("Key: "+ m.getKey() + ", Value: " + m.getValue()); } //bulk operations System.out.println("Clearing all the elements in map.."); map.clear(); System.out.println("Map Elements: " + map); } }
Note:
- Map.entrySet method returns a collection-view of the map.
- Map.Entry<Integer, String> is a map entry i.e., key-value pair
Output
Inserting elemens in map... Map Elements: {1=AB, 2=BC, 3=CD, 4=DE} Traversing through map elements and print key value pairs... Key: 1, Value: AB Key: 2, Value: BC Key: 3, Value: CD Key: 4, Value: DE Clearing all the elements in map.. Map Elements: {}
Related Posts
- Deque interface and ArrayDeque – Java Example
- Queue Interface and Example
- List Interface, Implementations and Examples
- Set Interface, Implementations and Examples