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.

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

posted

0

got it ragu. its hidden "magic" hu? regards maulin.

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.