This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What happens inside the set interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What happens inside the set interface" Watch "What happens inside the set interface" New topic
Author

What happens inside the set interface

Kaushik Baral
Greenhorn

Joined: Aug 08, 2009
Posts: 23
Hello all,
can someone explain what happens inside set interface (Collection) to avoid duplicate data.
thanks a lot in advance.
Joseph Mokenela
Ranch Hand

Joined: Jan 18, 2011
Posts: 59

I think the following link might shed some light.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Kaushik Baral wrote:Hello all,
can someone explain what happens inside set interface (Collection) to avoid duplicate data.
thanks a lot in advance.

Erm...nothing. An interface doesn't have any code. And Collection is NOT a Set, and doesn't provide any guarantees about uniqueness.

As to implementations, I suspect that it's down to the individual Set. From the documentation, it would appear that AbstractSet doesn't include any specific code to prevent duplicates, it merely requires that extensions "follow the rules".

It's fairly straightforward though: Set has a contains(Object) method which AbstractSet implements in a naive fashion (it actually uses AbstractCollection's, which simply iterates over all the elements in the Set looking for a matching element). It seems likely therefore that add(Object) could probably simply be implemented asand classes that need a faster version of contains() will override it with an implementation that makes sense for their particular type of Set.

The trouble is that that implementation is likely to be very specific to the type of Set, so it's virtually impossible to explain "what happens" unless we know what type of Set you're talking about.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
wood burning stoves
 
subject: What happens inside the set interface
 
Similar Threads
about collections
If set is interface then how it is instantiated:
Collection Interfaces
read( ) method
class in the interface