Set
About
The Set interface, part of the java.util package, is a specialized collection that does not allow duplicate elements. It models a mathematical set and is a child of the Collection interface. The primary purpose of a Set is to store unique elements in no particular order (unless specified by an implementation).
Introduced in Java 1.2 as part of the Java Collections Framework (JCF).
Commonly used to represent collections of distinct items, such as IDs, tags, or unique objects.
All implementations ensure that no two elements are equal as determined by their
equals()method.
Features
Unique Elements: A
Setcontains no duplicate elements, i.e., no two elements in the set are considered equal based on theequals()method.Optional Ordering:
HashSet: No ordering.LinkedHashSet: Maintains insertion order.TreeSet: Maintains natural or custom sorting order.
Efficient Membership Testing: The
contains()method is optimized in mostSetimplementations for quick membership testing.Null Handling:
HashSetandLinkedHashSetallow a singlenullelement.TreeSetdoes not allownullif it uses natural ordering (throwsNullPointerException).
Thread-Safety:
Basic implementations are not thread-safe.
Use
Collections.synchronizedSetorConcurrentSkipListSetfor thread-safe operations.
Functional Programming Support: From Java 8 onwards,
Setsupports methods likeforEach,stream, andremoveIffor functional-style operations.
Key Methods
Method
Description
add(E e)
Adds the specified element to the set if it is not already present.
remove(Object o)
Removes the specified element from the set if it is present.
contains(Object o)
Returns true if the set contains the specified element.
size()
Returns the number of elements in the set.
isEmpty()
Checks if the set is empty.
clear()
Removes all elements from the set.
iterator()
Returns an iterator over the elements in the set.
toArray()
Converts the set to an array.
addAll(Collection<? extends E>)
Adds all elements from the specified collection to the set.
retainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection.
removeAll(Collection<?> c)
Removes all elements in the set that are also contained in the specified collection.
stream()
Returns a sequential stream of the set’s elements.
Set Implementations
1. HashSet
HashSetFeatures:
Backed by a hash table.
No ordering of elements.
Allows a single
nullelement.
Performance:
O(1) for
add,remove, andcontainsin average cases.
Use Case: General-purpose set for quick lookups.
2. LinkedHashSet
LinkedHashSetFeatures:
Maintains insertion order.
Slightly slower than
HashSet.Allows one
nullelement.
Performance: Slightly higher overhead due to order tracking.
Use Case: When consistent iteration order is required.
3. TreeSet
TreeSetFeatures:
Implements
NavigableSet.Elements are stored in a sorted (natural or custom comparator) order.
Does not allow
nullelements.
Performance: O(log n) for
add,remove, andcontains.Use Case: Ordered collections and range-based operations.
4. EnumSet
EnumSetFeatures:
Specialized for
enumtypes.Very fast and memory-efficient (uses bitwise operations internally).
Performance: Faster than
HashSetforenumkeys.Use Case: Collections with predefined, fixed values.
5. ConcurrentSkipListSet
ConcurrentSkipListSetFeatures:
Thread-safe.
Sorted using natural order or a custom comparator.
Use Case: Multi-threaded applications where a sorted set is required.
6. CopyOnWriteArraySet
CopyOnWriteArraySetFeatures:
Thread-safe set based on a copy-on-write strategy.
Ideal for sets that are mostly read but occasionally updated.
Use Case: Concurrent read-heavy applications.
Custom Implementations of Set
Why Create a Custom Set?
Custom Set implementations are useful when:
We need custom equality or hashing logic.
Standard implementations are not optimized for specific scenarios.
Additional constraints or behaviors are required (e.g., a bounded set).
Steps to Implement a Custom Set
Implement the
SetInterface:The
Setinterface has 12 methods to implement.Extending
AbstractSetsimplifies the implementation by providing default behavior for some methods.
Key Considerations:
Storage: Decide how to store the elements (e.g., array, list, or tree).
Equality: Override
equals()andhashCode()methods.Concurrency: Decide whether thread-safety is needed.
Example: Fixed-Size Set
Use Case of Custom Set
Fixed-Size Sets: Ensure the number of elements does not exceed a certain limit.
Validation Logic: Enforce constraints on elements being added.
Last updated