This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on HashSet" Watch "Question on HashSet" New topic

Question on HashSet

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486

output :
Set contains --5
Set contains --5
Set contains --3

Why its returning me tow times 5 , i have used the equals and hashcode also.
Can anyone help me out ..

Thanks<br />Dinesh
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
You have to override hashCode() method (with capital C).
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Sorry my mistake:

Thanks for correcting.

If i pass null set.put(null), it gives me NullPointer Expection, i know nullponter means its trying to refer th object with null reference in memory.
Correct me if iam wrong
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
I guess you want '||' in your equals method instead of '&&'.

You get the NullPointerException on the line
System.out.println("Set contains --" + t.count);
You get null from the Set, so t == null, so t.count gives you the exception.
So you don't get the exception in the setting, but in the getting
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Question on HashSet
It's not a secret anymore!