Set Interface & Its Classes

introduction

Set Interface is provided by the collection framework. It represents an unordered collection of unique elements. Unique means that there are no duplicate elements. The difference between the List interface and the Set interface is that the list interface mainly works on ordered collection and performs operations using indexing methods.

As we said above, the Set interface works on unordered collections. Set Interface can not refer to more than one object. Unlikely list interfaces can refer to multiple objects. We can only store a maximum of one null value in the set. It does not contain any new methods which are in collection that methods only we can use in the set interface.

Set Interface consists of 4 classes.

  1. HashSet 
  2. LinkedHashSet
  3. EnumSet
  4. TreeSet 

What is Initial Capacity?

The capacity of the number of Buckets when Hashtable is created. It can increase automatically if the capacity gets full. The default value of Initial capacity is 16.

What is the Load Factor?

Load Factor is a measure “Till what load”, Hashmap can allow elements to put in it before its size is increased. The default value of the LoadFactor is 0.75.

1) HashSet:-

It was introduced in the 1.2 version. HashSet works on Hashtable data structure. Duplicates are not allowed. If we mistakenly add duplicate elements then it does not return any compile-time error or run time error it just returns false on add methods. Insertion order is not preserved in the HashSet. Heterogeneous objects are allowed in the HashSet. Search operation performs best in HashSet.

Constructor in HashSet:-

i) HashSet():- In this constructor, the default value of initial capacity is 16 and the default load factor is 0.75.

Eg. HashSet<E>HS=new HashSet<E>();

ii) HashSet(int initialCapacity):- It is in use to build an empty HashSet object in where initial capacity and the default values of load factor are 0.75.

Eg . HashSet<E>HS=new HashSet<E>(int initialCapacity)

iii)HashSet(int initialCapacity, float load factor ):-It is in use to build an empty HashSet object in which initial capacity is given as well as load factor also specifies.

Eg. HashSet<E>HS=new HashSet<E>(int initialCapacity, float loadFactor)

iv)HashSet(Collection):- This constructor is in use in order to get all elements from the collection.

Eg. HashSet<E>HS=new HashSet<E>(Collection)

Operation of Hash Set:-

Output:-

2) LinkedHashSet:-

It was introduced in the 1.4 version. We use HashSet to remove the duplicate values but the order is not maintain up. If we want to remove the duplicate values along with the maintained order we use LinkedHashedSet.It allows for Null values. Functions are similar to HashSet because it extends the HashSet class. By default, it is non-synchronize. The data structures involved in the HashSet are HashTable and Doubly Linked List. Using HashTable we remove duplicate elements and order get maintain by a doubly linked list.

Constructor in LinkedHashSet:-

i) HashSet():- In this constructor, the default value of initial capacity is 16 and the default load factor is 0.75.

Eg. HashSet<E>HS=new HashSet<E>();

ii) LinkedHashSet(int initialCapacity):-It is in use to build an empty Linkedhashset object in which initial capacity is ready and the default values of load factor are 0.75.

Eg . LinkedHashSet<E>HS=new LinkedHashSet<E>(int initialCapacity)

iii)HashSet(int initialCapacity, float load factor ):-It is in use to build an empty Linkedhashset object in which initial capacity is ready as well as load factor also specifies.

Eg.LinkedHashSet<E>HS=new LinkedHashSet<E>(int initialCapacity, float loadFactor)

iv)HashSet(Collection):-This constructor is in use to get all elements from the collection.

Eg. HashSet<E>HS=new HashSet<E>(Collection)

Operations of LinkedHashSet:-

Output:-

3) EnumSet:-

It was introduced in the 1.5 version. It extends AbstractSet class and implements the cloneable and serializable interface. The elements which are in the enumset belong to a single enumeration type. Null Objects are not permit by in the enumset. It is faster in speed because of its computation and it requires less memory and efficient for better results. It is faster than the HashSet.To implement enum we need to import the EnumSet package.

Methods in enumset class

i) clone():- To return a copy of a particular set.

ii) of(E a):- To create an enumset containing a single element.

iii) range(A from, A to):- To create an enumset containing a range of elements.

iv) allOf(Class Element Type):- To create an enumset containing all elements with the specified element type.

v) noneOf(Class Element Type):- To create an enumset with the specified element type.

Creation of Enumset Object:-

There are different implementations of EnumSet.

i) Regular EnumSet:- The elements store in a Long object which is 64 bits. Means 64 elements can store in RegularEnumSet

ii) JumboEnumSet:- The elements store in a Long array that contains more than 64 elements.

Operations of EnumSet:-

Output:-

4) TreeSet :-

It was introduced in the 1.2 version. The underlying data structure for TreeSet is Balanced Tree. Insertion order is not applicable. All elements inserted using Sorted order. Heterogenous objects are not permit. Null value insertion is permit but only once but after insertion of null if we try to insert another element then we will get NullPointerException. 

Constructor in TreeSet:-

i) TreeSet():- This constructor is in use to build an empty TreeSet.Using this constructor elements are store in default natural sorting order. 

Eg. TreeSet<E>TS=new TreeSet<E>();

ii) TreeSet(Comparator):- Constructor is in use to build an empty TreeSet object. Here we need external specification sorting order to store an element.

Eg.TreeSet<E>TS=new TreeSet<E>(Comparator C);

iii) TreeSet(Collection):- This Constructor is in use to build a TreeSet Object which contains all elements from Collection. These elements are store in default natural sorting order.

Eg.TreeSet<E>TS=new TreeSet<E>(Collection Co);

iv) TreeSet(SortedSet):- This Constructor is in use to build a TreeSet Object which contains all the elements from SortedSet and these elements get stored in default natural sorting order.

Eg.TreeSet<E>TS=new TreeSet<E>(SortedSet)

Operations of TreeSet:-

Output:-

Difference between Set ,List and Map Interface

Set:

i) Set does not contain duplicate elements.

ii) Set is an unordered collection.

iii) Set allows Null values but only once.

List:

i) List contain duplicate elements.

ii) List is an ordered collection.

iii) List allows Null values.

Map:

i) Maps consist of key-value pairs. It contains duplicate elements but keys always are unique.

ii) It provides Key-Value Pair.

iii) Map allows one Null key and  many Null values.

Conclusion:-

In Java, the collection framework provides the Set Interface. This Set interface came up with a lot of features we can do various operations on unordered elements. In this article, we successfully studied and discussed the Set Interface and its classes.

written by: Suraj Raut

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *