Set in Java
Introduction
A Set in Java is a collection that is used to store unique elements. Unlike a List, a Set does not allow duplicate values.
The Set interface is part of the Java Collection Framework and is available in the java.util package.
import java.util.Set;
Features of Set
- Does not allow duplicate elements.
- Stores unique values.
- Some Set implementations maintain insertion order.
- Supports dynamic size.
- Provides methods to add, remove, and search elements.
- Can store
nulldepending on the implementation.
Set Syntax
Set<Type> setName = new HashSet<>();
Example:
Set<String> names = new HashSet<>();
Example of Set
import java.util.*; public class SetExample { public static void main(String[] args) { Set<String> names = new HashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); names.add("Anu"); System.out.println(names); } }
Output
[Anu, Sita, Ram]
The duplicate "Anu" is not stored.
Note:
HashSetdoes not guarantee insertion order, so the output order may be different.
Common Set Methods
| Method | Description |
|---|---|
add() | Adds an element |
remove() | Removes an element |
contains() | Checks whether an element exists |
size() | Returns the number of elements |
isEmpty() | Checks whether the Set is empty |
clear() | Removes all elements |
addAll() | Adds elements from another collection |
containsAll() | Checks whether all specified elements exist |
Example of Set Methods
import java.util.*; public class SetMethods { public static void main(String[] args) { Set<Integer> numbers = new HashSet<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers.contains(20)); numbers.remove(10); System.out.println(numbers); System.out.println(numbers.size()); } }
Output
true [20, 30] 2
Types of Set
The commonly used Set implementations are:
1. HashSet
HashSet stores unique elements and does not guarantee any particular order.
Set<String> names = new HashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); System.out.println(names);
2. LinkedHashSet
LinkedHashSet maintains the insertion order of elements.
Set<String> names = new LinkedHashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); System.out.println(names);
Output
[Anu, Ram, Sita]
3. TreeSet
TreeSet stores unique elements in sorted order.
Set<Integer> numbers = new TreeSet<>(); numbers.add(30); numbers.add(10); numbers.add(20); System.out.println(numbers);
Output
[10, 20, 30]
Set vs List
| Set | List |
|---|---|
| Does not allow duplicates | Allows duplicates |
| Mainly used for unique elements | Used for ordered elements |
| Generally does not use indexes | Supports index-based access |
HashSet, LinkedHashSet, TreeSet | ArrayList, LinkedList, Vector |
TreeSet stores sorted elements | Maintains insertion order |
Set vs Array
| Set | Array |
|---|---|
| Dynamic size | Fixed size |
| Does not allow duplicates | Allows duplicates |
| Provides collection methods | Limited built-in methods |
| Part of Collection Framework | Basic Java structure |
| Useful for unique data | Useful for fixed-size data |
Real-Life Examples
A Set is useful when duplicate data should not be stored.
Examples:
- Unique student IDs
- Unique email addresses
- Unique usernames
- Unique phone numbers
- Unique product codes
- Unique city names
For example:
Student IDs ---------101 102 103 101
After storing them in a Set:
101 102 103
The duplicate 101 is automatically removed.
Advantages of Set
- Prevents duplicate values.
- Useful for storing unique data.
- Provides useful methods for searching and removing elements.
- Supports dynamic size.
TreeSetcan automatically sort elements.LinkedHashSetcan preserve insertion order.
Key Points
Setis an interface in Java.- It belongs to the
java.utilpackage. - A Set does not allow duplicate elements.
HashSetdoes not guarantee order.LinkedHashSetmaintains insertion order.TreeSetstores elements in sorted order.- Set is useful when uniqueness is important.
Conclusion
A Set is an important collection in Java for storing unique elements. It is especially useful when duplicate values must be avoided. The most commonly used implementations are HashSet, LinkedHashSet, and TreeSet.