- Map interface is not the child interface of collection interface.
- If you want to represent a group of objects as key value pair then we should go for map interface.
- In map both keys and values can be objects.
- In map duplicate keys are not allowed but values can be duplicate.
- Each key-value pair in Map is called an Entry. Hence called as collection of Entry Objects.
- Dictionary class, Hashtable and Properties class can in java 1.0 version.
- Map, HashMap and WeakHashMap came in java 1.2 version.
- IdentityHashMap and LinkedHashMap came in java 1.4 version.
- Dictionary class, Hashtable and Properties class can in java 1.0 version hence they are considered as legacy classes.
Implementation Class of Map interface :
- HashMap
- LinkedHashMap extends HashMap
- WeakHashMap
- IdentityHashMap
- Dictionary
- Hashtable extends Dictionary
- Properties extends Hashtable
- SortedMap<I>
- NavigableMap<I>
- TreeMap
Features of Map :
- When we need to represent data in the form of Key value paid then we should go for map
Map Interface Methods :
- Object put(Object key, Object value)
- Used to add one key value to the map
- If the key’s value is already present then the old value will be replaced with the new value and the old value will be returned
Example :
m.put("101","Tyson");//returns null m.put("102","Justin"); //returns null m.put("101","Martin"); //returns "Tyson"
- void putAll(Map m)
- Object get(Object key);
- Object remove(Object key);
- boolean containsKey(Object key)
- boolean containsValue(Object value)
- boolean empty()
- int size()
- void clear()
Collection views of Map
- Set keySet()
- Collection values()
- Set entrySet()
Entry <Interface>
- A map is a group of keyvalue pairs and each key value pair is called an Entry
- Hence map is considered as a collection of Entry Objects
- Without existing map object there is no chance of existing Entry Object hence Entry Interface is defined inside Map Interface
Entry specific methods and can only be applied on Entry Objects
- Object getKey();
- Object getValue();
- Object setValue(Object newValue);