This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet" Watch "TreeSet" New topic
Author

TreeSet

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi,
I read that Set interface doesn't allow duplicates in it. So, why does the following code works without any erros?

Should not 'ts' object thrwo exception if i try to insert two objects that have equlas() method returning true?
Please throw some light on this.
Regards
Maulin.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Maulin, Vasavada:
[B]Hi,
I read that Set interface doesn't allow duplicates in it. So, why does the following code works without any erros?

Should not 'ts' object thrwo exception if i try to insert two objects that have equlas() method returning true?
Please throw some light on this.
Regards
Maulin.[/B]

Yes. Set wont accept duplicates. But it wont throw any compile/runtime errors.
add returns a boolean so it returns FALSE if try to duplicate a value.. If you check the size of 'ts' it will be 1..
HIH
Ragu
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
got it ragu.
its hidden "magic" hu?
regards
maulin.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Nothing hidden or magical, just the definition of set. A set is a (usually unordered) collection of distinct values. You may be familiar with sets from math. As in a shape is the set of all points that make that shape, or the domain of the square root function is the set of all positive real numbers and zero. Values in a set are usually written in brackets (i.e. {1, 2, -5, 0.2}). The union or intersection of two sets contains only one of each distinct value (the union of {1, 2} and {2, 3} is {1, 2, 3}. The intersection would be {2}). You could consider Set.add() to be a funtion for determining the union of the existing set and an implied set containing only one value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet
 
Similar Threads
Comparable..
Collections-Getting error, using subList(fromIndex, toIndex) in List!
checking among a range of values
Natural ordering of strings and numbers
Sorting String List