wood burning stoves 2.0
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Duplication removal from Set" Watch "Duplication removal from Set" New topic

Duplication removal from Set

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1283

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

Joined: Mar 07, 2007
Posts: 1294

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.

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1283

Thanks Marco
I agree. Here's the link: http://aspose.com/file-tools
subject: Duplication removal from Set
It's not a secret anymore!