- Java Map interface represents the mapping betwixt unique keys to values.
- Map tin forcefulness out non concur duplicate values.
- In Map interface, Each fundamental tin forcefulness out map at close ane value.
- Each Map interface provides iii unlike collection views. Using which you lot tin forcefulness out sentiment map's content every bit a laid of keys, collection of values together with laid of key-value mappings.
- Map volition helps you lot when you lot wants to search, update or delete elements based on it's key.
- The map interface is implemented past times unlike coffee classes similar HashTable, HashMap together with TreeMap.
Map interface has below given unlike methods.
- void clear() : This method volition take all the mappings from this map.
- boolean containsKey(Object key) : It volition render truthful if map has mapping for specified key.
- boolean containsValue(Object value) : It volition render truthful if whatever fundamental of map has specified value.
- Set<Map.Entry<K,V>> entrySet() : It volition render laid sentiment of map.
- boolean equals(Object o) : It volition compare map alongside specified value for equality.
- V get(Object key) : It volition render value which is mapped alongside specified key. Return zippo if no mapping constitute for key.
- int hashCode() : It volition render hash code value of the electrical current map.
- boolean isEmpty() : It volition render truthful if no key-value mapping constitute inwards map.
- Set<K> keySet() : It volition render map keys inwards laid view.
- V put(K key, V value) : It volition lay specified value alongside specified fundamental inwards map.
- void putAll(Map<? extends K,? extends V> m) : It volition re-create all key-values mapping to this map.
- V remove(Object key) : It volition take mapping for specified fundamental from this map.
- int size() :It volition render full release of key-value mappings inwards this map.
- Collection<V> values() :It volition render collection sentiment of map values.
I accept prepared sample illustration to present you lot utilisation of unlike methods of Map interface.
Map Interface Example
package JAVAExamples; import java.util.HashMap; import java.util.Iterator; import java.util.Map; populace shape MapExample { populace static void main(String[] args) { // practise Map. Map<Integer, String> 1000 = novel HashMap<Integer, String>(); // Add fundamental alongside value inwards map. m.put(3,"three"); m.put(7,"seven"); m.put(1,"one"); m.put(5,"five"); m.put(2,"two"); m.put(8,"eight"); m.put(11,"eleven"); // Get size of map. System.out.println("Map size is : " + m.size()); //Check if map is empty. System.out.println("Map is empty? : "+m.isEmpty()); //Remove mapping from map. m.remove(3); System.out.println("Map value later removing mapping of fundamental 3 : "+m); //Get value from map for specified key. System.out.println("Value for fundamental 5 is : "+m.get(5)); //Iterating over map. Iterator<Integer> keySetIterator = m.keySet().iterator(); while(keySetIterator.hasNext()){ Integer fundamental = keySetIterator.next(); System.out.println("key: " + fundamental + " value: " + m.get(key)); } //Check if map contains specified key. System.out.println("Map has fundamental 7 ? : "+m.containsKey(7)); //Check if map contains specified value. System.out.println("Map has value xx ? : "+m.containsValue("twenty")); } }
Output :
Map size is : 7 Map is empty? : imitation Map value later removing mapping of fundamental 3 : {1=one, 2=two, 5=five, 7=seven, 8=eight, 11=eleven} Value for fundamental 5 is : 5 key: 1 value: ane key: 2 value: 2 key: 5 value: 5 key: 7 value: vii key: 8 value: 8 key: 11 value: 11 Map has fundamental 7 ? : truthful Map has value xx ? : false