A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction.
Defined in:
java.util.SetThe Set interface extends Collection and provides implementations that differ in:
- Ordering
- Performance
- Sorting behavior
- Null allowance
| Feature | Description |
|---|---|
| Duplicates | ❌ Not allowed |
| Null values | Allowed in HashSet & LinkedHashSet, not in TreeSet |
| Order maintained? | Depends on implementation |
| Index-based access? | ❌ No |
| Uses hashing/tree structures | Yes (depending on implementation) |
The main implementations are:
- HashSet
- LinkedHashSet
- TreeSet
- Stores elements in un-ordered form
- Uses hashing internally
- Allows 1 null value
- Best performance among all Set types
- Uses HashMap internally
- Elements stored as keys
- Dummy value is
PRESENT
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // ignored[Banana, Apple] (order may vary)
| Operation | Time |
|---|---|
| add | O(1) average |
| remove | O(1) |
| contains | O(1) |
| iteration | O(n) |
- Maintains insertion order
- Uses LinkedHashMap internally
- Allows 1 null value
- Slightly slower than HashSet
Set<String> set = new LinkedHashSet<>();
set.add("Java");
set.add("Python");
set.add("C++");[Java, Python, C++]
- Order matters
- You need HashSet performance with predictable traversal
- Sorted in ascending (natural) order
- Uses TreeMap (Red-Black Tree) internally
- Does not allow null
- Slower than HashSet & LinkedHashSet
Set<Integer> set = new TreeSet<>();
set.add(30);
set.add(10);
set.add(20);[10, 20, 30]
Set<String> set = new TreeSet<>(Comparator.reverseOrder());| Feature | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| Order | No | Insertion order | Sorted order |
| Performance | Fastest | Medium | Slowest |
| Null allowed | Yes | Yes | No |
| Internal Structure | HashMap | LinkedHashMap | TreeMap |
| Use When | Fast lookup | Fast + maintain order | Sorted data needed |
add(E e)
remove(Object o)
contains(Object o)
isEmpty()
size()
clear()
iterator()Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
System.out.println(set.contains(2)); // trueIterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}for (String s : set) {
System.out.println(s);
}set.forEach(System.out::println);List<Integer> list = Arrays.asList(1, 2, 2, 3, 1);
Set<Integer> set = new HashSet<>(list);
System.out.println(set); // [1, 2, 3]set1.addAll(set2);set1.retainAll(set2);set1.removeAll(set2);-
HashSet uses
hashCode()to compute bucket index -
If collision happens → stored in a LinkedList or TreeNode
-
Java 8+ converts long collision chains to balanced trees
-
equals()is used to check duplicates
Because it checks equality using equals() and hashCode().
LinkedHashSet and TreeSet.
Because hashing provides O(1) operations on average.
No, because it needs to compare elements.
TreeSet.
They are ignored.
-
Set does not allow duplicates.
-
Use
HashSetfor fast access. -
Use
LinkedHashSetfor predictable iteration. -
Use
TreeSetfor sorted data. -
Internal workings depend on HashMap/TreeMap.
-
No indexing → must use iterators for traversal.