Wednesday, 29 August 2012

Java Map basics


       Map<String, String> map = new HashMap<>();

       map.put("One", new Integer(1));    
       map.put("Two", new Integer(2));
       map.put("Three", new Integer(3));

     
      Retriving the values from map by passing perticular key    
      Object one =  map.get("One");       
      System.out.println("Value is " + one);

      Validating key from map by passing perticular value

      if( map.containsKey("One") ){
            System.out.println("HashMap contains One as key");
        }else{
            System.out.println("HashMap does not contain One as value");
        }
  
     Validating value from map by passing perticular value
         if(hashMap.containsValue(new Integer(1))){
            System.out.println("HashMap contains 1 as value");
        }else{
           System.out.println("HashMap does not contain 1 as value");        
    }

     Retriving All values from map. 
      Iterator entries = map.entrySet().iterator();
      Entry thisEntry = (Entry) entries.next();
      Object value = thisEntry.getValue().equals("
One");
      Object key= thisEntry.getKey();
      System.out.println(value+""+ thisEntry.getKey().equals("One"));

No comments:

Post a Comment