aspose file tools*
The moose likes Java in General and the fly likes Duplication removal from Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Duplication removal from Set" Watch "Duplication removal from Set" New topic
Author

Duplication removal from Set

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

How set collection manages to remove duplicate objects from it. If entry of every object checked by equals from all present object then it is a very lengthy process to do it. Is it by the same way or some other way around?


No Kaustubh No Fun, Know Kaustubh Know Fun..
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

The HashSet for example just uses an internal HashMap to manage its elements. That way it's possible to guarantee that certain operations only take constant time, i.e. it's equally fast, no matter how big the set may grow. The API documentation of HashSet says the following:
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.


Basically that's possible because HashMap uses the hashCode() method to handle its elements which makes it unnecessary (depending on the quality of the produced hashes) to compare new elements to each existing element.

Marco
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Thanks Marco
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Duplication removal from Set