Java Map Interface

  • Java Map interface represents the mapping between unique keys to values.
  • Map can not hold duplicate values.
  • In Map interface, Each key can map at most one value.
  • Each Map interface provides three different collection views. Using which you can view map's content as a set of keys, collection of values and set of key-value mappings.
  • Map will helps you when you wants to search, update or delete elements based on it's key.
  • The map interface is implemented by different java classes like HashTable, HashMap and TreeMap.
Map interface has below given different methods.
  • void clear() : This method will remove all the mappings from this map.
  • boolean containsKey(Object key) : It will return true if map has mapping for specified key.
  • boolean containsValue(Object value) : It will return true if any key of map has specified value.
  • Set<Map.Entry<K,V>> entrySet() : It will return set view of map.
  • boolean equals(Object o) : It will compare map with specified value for equality.
  • V get(Object key) : It will return value which is mapped with specified key. Return null if no mapping found for key.
  • int hashCode() : It will return hash code value of the current map.
  • boolean isEmpty() : It will return true if no key-value mapping found in map.
  • Set<K> keySet() : It will return map keys in set view.
  • V put(K key, V value) : It will put specified value with specified key in map.
  • void putAll(Map<? extends K,? extends V> m) : It will copy all key-values mapping to this map.
  • V remove(Object key) : It will remove mapping for specified key from this map.
  • int size() :It will return total number of key-value mappings in this map.
  • Collection<V> values() :It will return collection view of map values.
I have prepared sample example to show you usage of different methods of Map interface.

Map Interface Example
package JAVAExamples;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapExample {

 public static void main(String[] args) {
  // create Map.
  Map<Integer, String> m = new HashMap<Integer, String>();
  // Add key with value in 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 after removing mapping of key 3 : "+m);
  
  //Get value from map for specified key.
  System.out.println("Value for key 5 is : "+m.get(5));
  
  //Iterating over map.
  Iterator<Integer> keySetIterator = m.keySet().iterator();
  while(keySetIterator.hasNext()){
    Integer key = keySetIterator.next();
    System.out.println("key: " + key + " value: " + m.get(key));
  }

  //Check if map contains specified key.
  System.out.println("Map has key 7 ? : "+m.containsKey(7));

  //Check if map contains specified value.
  System.out.println("Map has value twenty ? : "+m.containsValue("twenty"));
 }
}

Output :
Map size is : 7
Map is empty? : false
Map value after removing mapping of key 3 : {1=one, 2=two, 5=five, 7=seven, 8=eight, 11=eleven}
Value for key 5 is : five
key: 1 value: one
key: 2 value: two
key: 5 value: five
key: 7 value: seven
key: 8 value: eight
key: 11 value: eleven
Map has key 7 ? : true
Map has value twenty ? : false

No comments:

Post a Comment