• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeSet

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it ragu.
its hidden "magic" hu?
regards
maulin.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic