Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it ragu.
its hidden "magic" hu?
regards
maulin.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic