Set Interface

0
553

So far we have discussed about the Collection Interface and List Interface & its implementation Classes.

Now we will move further to discuss the next important interface in Collection API which does not allow duplicate elements in any of its implementation. Let’s have a look:

Set Interface

Java.util.Set implementations do not allow duplicates. So each element can exist only once in any of the Set implementation.
set_interface_new

In here, equals() determine if 2 objects are identical or not and with the help of that duplicate elements in Set classes are avoided.

1) HashSet

HashSet is an Unsorted and Unordered Set implementation. It uses hashcode of object which is being inserted. So more efficient hashCode() implementation provides better access performance.

Generally it is suggested that you use it when you don’t care about the order when you iterate through.

There are four constructors provided for HashSet:

HashSet() – Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection c) – Constructs a new set containing the elements in the specified collection.
HashSet(int initialCapacity) – Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor) – Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

2) LinkedHashSet

LinkedHashSet is an Ordered version of HashSet. It is actually a direct subclass of HashSet. It maintains a doubly-linked list running through all of its entries.

Note that this implementation is not synchronized. Also, when using HashSet or LinkedHashSet, the objects added to them must override hashcode() else default Object.hashCode() method will allow duplicate objects.
As this is an ordered implementation of Set interface, we can use it when we want to get the elements in the same order in which they were inserted.

Four constructor of LinkedHashSet are:

LinkedHashSet() – Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
LinkedHashSet(Collection c) – Constructs a new linked hash set with the same elements as the specified collection.
LinkedHashSet(int initialCapacity) – Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
LinkedHashSet(int initialCapacity, float loadFactor) – Constructs a new, empty linked hash set with the specified initial capacity and load factor.

3) TreeSet

TreeSet is another quite a popular and one of the most widely used collection class.

It is a Sorted Collection. Optionally, its constructor allows to specify your own rules for ordering by using a Comparable or Comparator as well. If you attempt to add elements which don’t implement compareTo() method, ClassCastException occurs.

These are the constructors for TreeSet :

TreeSet() – Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
TreeSet(Collection c) – Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
TreeSet(Comparator comparator) – Constructs a new, empty tree set, sorted according to the specified comparator.
TreeSet(SortedSet s) – Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

In the next post after this, I would discuss the Map interface and its implementing classes.

Previous articleList Interface
Next articleMap Interface
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY