- 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 :
- LinkedHashMap extends HashMap
- Hashtable extends Dictionary
- Properties extends Hashtable
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
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()
- 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);