This discussion is archived
7 Replies Latest reply: May 31, 2011 2:52 AM by DarrylBurke RSS

map.get returns unexpected null

865539 Newbie
Currently Being Moderated
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points