Java – Map Interface

Map:

  • Store values using keys
    • Map = {key=value, key=value, key=value ….}
  • Keys must be unique in Map
  • Values can be duplicated.
  • For example, book names(unique) with prices(duplicates)
    • Books = {C=300.0 , C++=300.0, Java=350.0, Python=330.0};

Methods are:

MethodDescription
put(K key, V value)store value using key.
V get(Object key)return value of specified key.
boolean isEmpty()Returns true if this map contains key-values.
void clear()Removes all elements.
boolean containsKey(Object key)Returns true if map contains specified key.
Set<K> keySet()Returns a Set of keys contained in this map.
remove(Object key)Removes key-value of specified key.
replace(K key, V value)Replace the value of specified key with given value.
int size()Returns the number of key-values in map.
Collection<V> values()Returns Collection of values in this map.

Program to create HashMap and display:

import java.util.*;
class Code
{
            public static void main(String[] args)
            {
                        Map<Integer,String> map = new HashMap<Integer,String>();
                        map.put(10, “Ten”);
                        map.put(20, “Twenty”);
                        map.put(30, “Thirty”);
                        map.put(40, “Fourty”);
                        map.put(50, “Fifty”);
                        System.out.println(“Map : ” + map);
            }
}

Implementations of Map:

  1. HashMap: doesn’t maintain insertion order. Allows only one null key
  2. LinkedHashMap: maintains insertion order. Allows only one null key
  3. TreeMap: maintains sorted order of keys. It doesn’t allow null key.
  4. Hashtable: It is call legacy class. It maintains sorted order. It doesn’t allow null key.
import java.util.*;
class Code
{
            public static void main(String[] args) {
                        //Map<Integer,String> map = new LinkedHashMap<Integer,String>();
                        Map<Integer,String> map = new TreeMap<Integer,String>();
                        map.put(50, “Fifty”);
                        map.put(40, “Fourty”);
                        map.put(30, “Thirty”);
                        map.put(20, “Twenty”);
                        map.put(10, “Ten”);
                        System.out.println(“Map : ” + map);
            }
}

Set<K> keySet():

  • We cannot iterate the map object either by using Iterator or using for-each loop.
  • First we need to collect all keys of map using keySet() method.
  • We iterate keys set and get values by specifying each key.
import java.util.*;
class Code
{
           public static void main(String[] args) throws Exception {
                       Map<Integer, String> map = new HashMap<Integer, String>();
                       map.put(10, “Ten”);
                       map.put(20, “Twenty”);
                       map.put(30, “Thirty”);
                       map.put(40, “Fourty”);
 
                       System.out.println(“Map is : “);
                       Set<Integer> keys = map.keySet();
                       for(Integer key : keys){
                                   String value = map.get(key);
                                   System.out.println(key + ” = ” + value);
                       }
           }
}

Iterate Map through Iterator:

  • Collect the keys using keySet() method.
  • Create Iterator from the keys Set.
  • Iterate the object and get Values by specifying keys.
import java.util.*;
class Code
{
            public static void main(String[] args) throws Exception {
                        String[] books = {“C”, “C++”, “Java”, “Python”, “Android”};
                        double[] prices = {200.0, 300.0, 250.0, 200.0, 250.0};
 
                        Map<String, Double> map = new HashMap<String, Double>();
                        for(int i=0 ; i<=books.length-1 ; i++) {
                                    map.put(books[i], prices[i]);
                        }
           
                        System.out.println(“Map is : “);
                        Set<String> keys = map.keySet();
                        Iterator<String> itr = keys.iterator();
                        while(itr.hasNext()) {
                                    String key = itr.next();
                                    Double value = map.get(key);
                                    System.out.println(key + ” = ” + value);
                        }
            }
}
Scroll to Top