Thursday 13 February 2014

Playing with Map

My first time confusion about Map :

  1. Map is a interface not a class, so we can't create a Object of Map.
  2. Map are quite different from List , they contains key - value pair.
   import java.util.Collection;
   import java.util.HashMap;
   import java.util.Map;
   import java.util.Set;


      public class TestClass implements Map<String,String>{
          public static void main(String ...strings){
              Map<String,String> map = new HashMap<String,String>();
              map.put("1" , "first");
              map.put("2", "second");
              map.put("2", null); Map can't have duplicate elements last value override previous value.
              map.put(null, null); Key and Value both can be null.
         System.out.println(map);
         System.out.println(map.size());
     
    }
}

Output : 

{null=null, 2=null, 1=first}
3

No comments:

Post a Comment