Choosing the Right Set Implementation for Your Needs in Java

Uğur Taş
Codimis
Published in
5 min readSep 6, 2023

--

Set of Trees
Photo by Johannes Plenio on Unsplash

We explored ArrayList and LinkedList in the previous article. So it became necessary to compare set implementations. When it comes to storing a collection of unique elements, two popular choices are HashSet and TreeSet. Both of these implementations belong to the java.util package and offer distinct characteristics, advantages, and use cases. In this article, we will explore HashSet and TreeSet, moreover try to help you make informed decisions when choosing the proper set implementation for specific requirements of your application.

If you don’t have a medium membership, you can use this link to reach the article without a paywall.

Description of the HashSet and TreeSet

HashSet

A HashSet in Java is a collection that does not allow duplicate elements. This collection type is implemented using a hash table, which provides fast insertion, deletion, and retrieval operations. Internally, each element is associated with a hash code, which determines its position within the hash…

--

--

Uğur Taş
Codimis

A software developer with a high passion for learning, improving skills, and sharing knowledge. My motto is “fail million times if you take lessons every time”