Java Set

Collections are objects that hold other objects which are maintained under some set of rules.

Java Set

Java Set

     

Collections are objects that hold other objects which are maintained under some set of rules. A set is a public interface that extends the collection interface and comes under the collection framework It holds unique values means set does not contain duplicate elements. Collection interface is at the top of collection hierarchy and Set interface in "java.util" package is it's sub-class. It has two useful concrete classes that implements sets:

  1. java.util.HashSet: This class is implemented with a hash table. Entries are unsorted and no guarantees for the iteration order. This class permits the null element.
  2. java.util.TreeSet: This class is implemented as a balanced binary tree. Entries are sorted. This implementation is not synchronized.

Note: Great care must be exercised while mutable objects are stored in the set. The behavior of a set is not predictable while an object is changed in a manner that affects equals comparisons while the object is an element in the set. For this a special case is that a set does not allow to contain itself as an element.

Learn more on the set interface at:

http:/www.roseindia.net/java/java-tips/data/collections/sets/sets.shtml