Set Interface
The Set interface is part of the Java Collections Framework and represents a collection that does not allow duplicate elements. It models the concept of a mathematical set, ensuring uniqueness of elements. This is a high-frequency interview topic, often compared with List.
What Is the Set Interface?
- A collection that does not allow duplicates
- No index-based access
- Order depends on implementation
- Allows at most one null (implementation-dependent)
- Part of java.util package
Set<String> set = new HashSet<>();
Position in Collection Hierarchy
Iterable
└── Collection
└── Set
├── HashSet
├── LinkedHashSet
└── TreeSet
Key Characteristics of Set
- No duplicate elements
- Equality based on equals() and hashCode()
- No positional (index) access
- Designed for uniqueness and fast lookup
Common Implementations of Set
1️⃣ HashSet
- Backed by hash table
- Unordered (no guarantee of order)
- Fast performance (O(1) average)
- Allows one null
Set<Integer> set = new HashSet<>();
2️⃣ LinkedHashSet
- Maintains insertion order
- Slightly slower than HashSet
- Allows one null
Set<Integer> set = new LinkedHashSet<>();
3️⃣ TreeSet
- Stores elements in sorted order
- Based on Red-Black Tree
- Does not allow null
- Slower (O(log n) operations)
Set<Integer> set = new TreeSet<>();
Important Methods of Set Interface
set.add("Java");
set.remove("Java");
set.contains("Java");
set.size();
set.isEmpty();
- ✔ No get(index)
- ✔ No positional methods
Duplicate Handling Example
Set<String> set = new HashSet<>();
set.add("Java");
set.add("Java");
System.out.println(set); // [Java]
- ✔ Duplicate ignored
- ✔ No exception thrown
How Set Ensures Uniqueness (Interview Favorite)
- Uses hashCode() to find bucket
- Uses equals() to check equality
class Employee {
int id;
}
⚠️ If equals() and hashCode() are not overridden properly, duplicates may occur logically.
Set vs List (Interview Comparison)
| Aspect | Set | List |
|---|---|---|
| Duplicates | ❌ Not allowed | ✅ Allowed |
| Order | Depends on implementation | Preserved |
| Index access | ❌ No | ✅ Yes |
| Use case | Unique data | Ordered data |
Performance Complexity
| Implementation | Add / Remove / Search |
|---|---|
| HashSet | O(1) average |
| LinkedHashSet | O(1) |
| TreeSet | O(log n) |
Iterating a Set
for (String s : set) {
System.out.println(s);
}
or
Iterator<String> it = set.iterator();
When to Use Set
- When uniqueness is required
- Removing duplicates
- Membership checking
- Mathematical set operations
When NOT to Use Set
- When duplicates are needed
- When index-based access is required
- When strict insertion order is mandatory (unless LinkedHashSet)
Common Beginner Mistakes
- Expecting insertion order in HashSet
- Using mutable objects as keys without proper equals() / hashCode()
- Trying to access elements by index
- Using TreeSet with null values
Interview-Ready Answers
Short Answer
The Set interface represents a collection that does not allow duplicate elements.
Detailed Answer
In Java, the Set interface extends the Collection interface and models a mathematical set. It ensures uniqueness of elements and provides implementations such as HashSet, LinkedHashSet, and TreeSet, each with different ordering and performance characteristics.
Key Takeaway
Use Set when uniqueness matters. Choose the right implementation (HashSet, LinkedHashSet, or TreeSet) based on ordering and performance needs.