set interface

The Java Set Interface, Implementations and Examples

The Set interface is a collection that cannot contain duplicate elements. It models the mathematical set abstraction and is used to represent sets like below

  • Cards comprising a poker hand
  • Courses making up the schedule of a student
  • The processes running on a machine

The Set interface contains only methods inherited from Collection and adds the restriction to prohibit the duplicate elements.

“Set” interface Java SE 8

“add” Operation of Set Interface

Adds the specified element to the set if it is not already present and this is optional operation. If the set already contains the element, the call to this method leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

“equals” Operation of Set Interface

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.

Implementation of “Set” Interface

The implementations of Set interface are:

  • HashSet – It stores its elements in a hash table and it is the best performing implementation and faster than TreeSet. But there is no guarantee on the order of iteration. It is the most commonly used implementation.
  • TreeSet – It stores its elements in a red-black tree and orders its elements based on their values i.e., elements will be in ascending order, according to natural order. It is slower than HashSet.
  • LinkedHashSet – It is implemented as a hash table with a linked list running through it. It maintains insertion-order i.e., the order in which the elements are inserted in to the set.

Limitations of Using HashSet

One thing that you should keep in mind about HashSet is that “iteration is linear in the sum of the number of entries and the number of buckets (the capacity)”.

Thus choosing an initial capacity that is too high, can waste both space and time. On the other hand choosing an initial capacity that is too low wastes time by copying data structure each time it’s forced to increase the capacity.

If you don’t specify the initial capacity, then the default value is 16. In the past, there were some advantages when you choose prime number as initial capacity. But now it’s no longer true, because internally the capacity is always rounded up to a power of 2.

The following line of code allocates a HashSet whose initial capacity is 64

Example: Set Interface and HashSet

Output:

Example: Set Interface and TreeSet

Output:

Example: Set Interface and LinkedHashSet

Output:

Special-Purpose Set Implementations

There are two special-purpose Set implementations

  1. EnumSet – It is a high-performance Set implementation for enum types.All of the members of an enum set must be of the same enum type.
  2. CopyOnWriteArraySet – It is a Set implementation backed up by a copy-on-write array. All mutative operations, such as add, set, and remove, are implemented by making a new copy of the array; no locking is ever required.

Related Posts

References

Leave a Reply

avatar
  Subscribe  
Notify of