Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

map.get returns unexpected null

865539May 31 2011 — edited May 31 2011
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2011
Added on May 31 2011
7 comments
151 views