• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet problem  RSS feed

 
Sudhakar Reddy Kurakula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String a="sudhakar";
String b=new String("sudhakar");
HashSet hs=new HashSet();
hs.add(new String("sudha"));
hs.add(new String("sudha"));
hs.add(a);
hs.add(b);
hs.add("reddy");
hs.add("reddy");

Now I want to know how many objects will be in Hashset?

Cheers
Sudha.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems simple enough to just try it, eh? Displaying "hs.size()" would tell you the answer.

But the answer is "3". By definition a Set eliminates duplicates (objects that are equal), and Strings are equal if they contain the same characters in the same order, so "3" follows. Are you having trouble understanding why?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!