๐ Java Collections - Understanding Set Interface with Real-World Use Cases


Series: Java Collections Framework | Part 1 โ Set
Hello Devs! ๐
Welcome to the first blog of my Java Collections Framework series. In this post, weโll deep-dive into the Set
interfaceโits purpose, types, when to use which type, and solve a real-world problem using each variant.
๐ง What is a Set
in Java?
A Set
is a collection that does not allow duplicate elements. It models the mathematical set abstraction, used when we want to store unique values.
Key Features:
No duplicates allowed.
May or may not maintain insertion order (depends on the implementation).
Can store at most one null element (in most implementations).
๐ก Real-World Problem Statement
โYou're building a contact management system. You need to store a list of email addresses, but duplicates should be removed automatically. Depending on your UI/UX design, you might want to preserve the order of entry or sort the emails alphabetically.โ
๐ง Use Case 1: Remove Duplicate Emails with No Order Guarantee
Solution: Use HashSet
๐ธ HashSet
:
Does not maintain insertion order.
Offers constant-time performance for basic operations like add, remove, and contains.
Allows one
null
element.
โ Code Example:
import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
public static void main(String[] args) {
Set<String> emails = new HashSet<>();
emails.add("suraj@gmail.com");
emails.add("info@example.com");
emails.add("suraj@gmail.com"); // Duplicate
emails.add("hello@domain.com");
System.out.println("Unique Emails: " + emails);
}
}
๐ Output:
Unique Emails: [info@example.com, suraj@gmail.com, hello@domain.com]
โ ๏ธ Notice: Order is not guaranteed.
๐ง Use Case 2: Preserve Insertion Order of Emails
Solution: Use LinkedHashSet
๐ธ LinkedHashSet
:
Maintains the order in which elements were inserted.
Slightly slower than
HashSet
due to the linked list used for ordering.Still provides constant-time performance.
โ Code Example:
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetExample {
public static void main(String[] args) {
Set<String> emails = new LinkedHashSet<>();
emails.add("suraj@gmail.com");
emails.add("info@example.com");
emails.add("suraj@gmail.com"); // Duplicate
emails.add("hello@domain.com");
System.out.println("Ordered Emails: " + emails);
}
}
๐ Output:
Ordered Emails: [suraj@gmail.com, info@example.com, hello@domain.com]
โ Maintains the insertion order.
๐ง Use Case 3: Store Emails in Alphabetical Order
Solution: Use TreeSet
๐ธ TreeSet
:
Stores elements in sorted (natural) order.
Does not allow null values.
Backed by a Red-Black tree, so operations take
O(log n)
time.
โ Code Example:
import java.util.Set;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
Set<String> emails = new TreeSet<>();
emails.add("suraj@gmail.com");
emails.add("info@example.com");
emails.add("hello@domain.com");
emails.add("suraj@gmail.com"); // Duplicate
System.out.println("Sorted Emails: " + emails);
}
}
๐ Output:
Sorted Emails: [hello@domain.com, info@example.com, suraj@gmail.com]
โ Automatically sorted alphabetically.
๐ When to Use What?
Set Type | Maintains Order? | Allows Null | Use Case |
HashSet | โ No | โ Yes | Fast access, no order needed |
LinkedHashSet | โ Insertion order | โ Yes | Preserve input order |
TreeSet | โ Sorted order | โ No | Automatically sorted unique items |
๐ฏ Summary
Set
is used for unique elements.Choose the type of
Set
based on your needs:Unordered:
HashSet
Ordered (insertion):
LinkedHashSet
Ordered (sorted):
TreeSet
๐ Coming Up Next
In the next part of this series, weโll explore the List
interface and its implementations like ArrayList
, LinkedList
, and more with real-life use cases.
Stay tuned and follow me for more posts on Java Collections Framework!
Let me know in the comments if you want a comparison between Set
and List
, or anything specific about Collections ๐
Subscribe to my newsletter
Read articles from Suraj Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Suraj Shinde
Suraj Shinde
I'm Suraj Parmeshwar Shinde, a passionate software developer from Tadshivani, Maharashtra, currently based in Pune. Iโve recently completed my Bachelor of Computer Applications (BCA) from Badrinarayan Barwale College, Jalna. During my graduation, I worked as a Software Intern at PRYM Aerospace Pvt. Ltd., where I contributed to the development of an AI-based crop advisory platform using technologies like Node.js, Flask, and React.js. This experience helped me gain hands-on knowledge of real-world software development and agile practices. For my final year project, I built Medicheck, a full-stack doctor appointment booking system using the MERN stack and Tailwind CSS. It features patient and admin panels, doctor profiles, secure Razorpay payments, and a mobile-responsive interface. My core technical skills include React.js, Node.js, Express.js, JavaScript, Java, MongoDB, SQL, and tools like Git, Postman, Docker, and Netlify. Iโm a quick learner who enjoys building real-world applications, solving logical problems, and writing clean, maintainable code. Outside of tech, I enjoy driving and reading books. Iโm always eager to grow, collaborate, and contribute to impactful technology solutions.