Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

TreeSet  RSS feed

 
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.
 
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.
 
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
Boost this thread!