7 Replies Latest reply: May 31, 2011 4:52 AM by Darryl Burke RSS

    map.get returns unexpected null

    865539
      I had written 2 classes and there output is also given. i would like to know the reason for null and any modification which i can do to get the value from map.
      public class TestBean {
      
           String str=null;
           TestBean(String str)
           {
                this.str=str;
           }
      }
      
      
      public class Test {
           public static void main(String[] args) {
                Map map = new HashMap();
                TestBean t = new TestBean("junk");
      
                map.put(t, "buster");
                System.out.println(map.get(t));
      
                map.put(new TestBean("junk"), "buster");
                System.out.println(map.get(new TestBean("Harsh")));
           }
      }
      OUTPUT
      buster
      null

      Edited by: EJP on 31/05/2011 17:31: corrected your title and added {noformat}
      {noformat} tags