Java Collection Frameworks

Lochana Edirisinghe
2 min readMar 26, 2020

Hello guys,

Today I am gonna explain about Set classes in collection frameworks of Java.

In collection frameworks, you can use built-in functionalities to search the data, sort the data, insertion or deletion and lots of operations. In traditional data structures, you need to write those methods yourself. But here you can get methods which can do the task for you effectively and efficiently.

Collections are containers that group multiple items in a single unit. There are many collection interfaces & classes in java. We can divide those classes and interfaces as follows.

class diagram

Classes that are implemented by List interface have indexing. There is an insertion order. But Classes which are implemented by Set interface don’t have indexing. There is no insertion order.

Set Classes

The main advantage of Set classes is removing duplicate elements. If we put objects that we prepared into set classes we must override the hashcode method and equal method.

Set classes can remove their duplicates by overriding only equals method. But think if a HashSet has a thousand object and then when you add the next object, it has to compare with thousand equal methods. This is inefficient and that’s why the hashcode method is here. Firstly hashcode method checks whether two objects are equal or not using hashcode and if equal, again the hashcode method calls the equal method of the object and checks the equality. If it is true, then that object isn’t added to the HashSet. This mechanism is called Hashing. See example

I hope you got some idea about the purpose of Set classes. We will discuss about all the other collection classes in near future. Thank you!!

--

--