File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question About TreeSet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question About TreeSet?" Watch "Question About TreeSet?" New topic

Question About TreeSet?

Afzal Rehman
Ranch Hand

Joined: Dec 16, 2009
Posts: 39

Why Output is 1? ,

SCJP 6.0
Henry Wong

Joined: Sep 28, 2004
Posts: 20366

First, you example uses a HashSet -- not a TreeSet.... To answer your question, Sets don't allow duplicates.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Add method will return false if the object it is adding to the set is already there in the set.
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
HashSet won't allow duplicates.
I agree. Here's the link:
subject: Question About TreeSet?
jQuery in Action, 3rd edition