Home » JAVA Programming » Collections » Question
  1. What is the output of below snippet?
    import java.util.*;
    public class Map_Example
    {
    public static void main(String[] args)
    {
    Map mapSample = new TreeMap();
    mapSample.put(4, null);
    mapSample.put(0, null);
    mapSample.put(3, null);
    mapSample.put(2, null);
    mapSample.put(1, null);

    System.out.println(mapSample);
    }
    }
    1. {0=null, 1=null, 2=null, 3=null, 4=null}
    2. Runtime Error
    3. Compilation Error
    4. {3=null, 0=null, 2=null, 1=null, 4=null}
    5. None of these
Correct Option: A

HashMap needs unique keys. TreeMap sorts the keys while storing objects.



Your comments will be displayed only after manual approval.