Class CaseInsensitiveSet<E>
- Type Parameters:
E
- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
Set
implementation that performs case-insensitive comparisons for String
elements,
while preserving the original case of the strings. This set can contain both String
and non-String elements,
providing support for homogeneous and heterogeneous collections.
Key Features
- Case-Insensitive String Handling: For
String
elements, comparisons are performed in a case-insensitive manner, but the original case is preserved when iterating or retrieving elements. - Homogeneous and Heterogeneous Collections: Supports mixed types within the set, treating non-String
elements as in a normal
Set
. - Customizable Backing Map: Allows specifying the underlying
Map
implementation, providing flexibility for use cases requiring custom performance or ordering guarantees. - Compatibility with Java Collections Framework: Fully implements the
Set
interface, supporting standard operations likeadd()
,remove()
, andretainAll()
.
Usage Examples
// Create a case-insensitive set
CaseInsensitiveSet<String> set = new CaseInsensitiveSet<>();
set.add("Hello");
set.add("HELLO"); // No effect, as "Hello" already exists
System.out.println(set); // Outputs: [Hello]
// Mixed types in the set
CaseInsensitiveSet<Object> mixedSet = new CaseInsensitiveSet<>();
mixedSet.add("Apple");
mixedSet.add(123);
mixedSet.add("apple"); // No effect, as "Apple" already exists
System.out.println(mixedSet); // Outputs: [Apple, 123]
Backing Map Selection
The backing map for this set can be customized using various constructors:
- The default constructor uses a
CaseInsensitiveMap
with aLinkedHashMap
backing to preserve insertion order. - Other constructors allow specifying the backing map explicitly or initializing the set from another collection.
Deprecated Methods
The following methods are deprecated and retained for backward compatibility:
plus()
: UseaddAll(Collection)
instead.minus()
: UseremoveAll(Collection)
instead.
Additional Notes
- String comparisons are case-insensitive but preserve original case for iteration and output.
- Thread safety depends on the thread safety of the chosen backing map.
- Set operations like
contains()
,add()
, andremove()
rely on the behavior of the underlyingCaseInsensitiveMap
.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyCaseInsensitiveSet
backed by aCaseInsensitiveMap
with a defaultLinkedHashMap
implementation.CaseInsensitiveSet
(int initialCapacity) Constructs an emptyCaseInsensitiveSet
with the specified initial capacity.CaseInsensitiveSet
(int initialCapacity, float loadFactor) Constructs an emptyCaseInsensitiveSet
with the specified initial capacity and load factor.CaseInsensitiveSet
(Collection<? extends E> collection) Constructs aCaseInsensitiveSet
containing the elements of the specified collection.CaseInsensitiveSet
(Collection<? extends E> source, Map backingMap) Constructs aCaseInsensitiveSet
containing the elements of the specified collection, using the provided map as the backing implementation. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) void
clear()
boolean
boolean
containsAll
(Collection<?> c) boolean
int
hashCode()
boolean
isEmpty()
iterator()
Deprecated.Deprecated.Deprecated.Deprecated.boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.util.Set
spliterator
-
Constructor Details
-
CaseInsensitiveSet
public CaseInsensitiveSet()Constructs an emptyCaseInsensitiveSet
backed by aCaseInsensitiveMap
with a defaultLinkedHashMap
implementation.This constructor is useful for creating a case-insensitive set with predictable iteration order and default configuration.
-
CaseInsensitiveSet
Constructs aCaseInsensitiveSet
containing the elements of the specified collection.The backing map is chosen based on the type of the input collection:
- If the input collection is a
ConcurrentNavigableSetNullSafe
, the backing map is aConcurrentNavigableMapNullSafe
. - If the input collection is a
ConcurrentSkipListSet
, the backing map is aConcurrentSkipListMap
. - If the input collection is a
ConcurrentSet
, the backing map is aConcurrentHashMapNullSafe
. - If the input collection is a
SortedSet
, the backing map is aTreeMap
. - For all other collection types, the backing map is a
LinkedHashMap
with an initial capacity based on the size of the input collection.
- Parameters:
collection
- the collection whose elements are to be placed into this set- Throws:
NullPointerException
- if the specified collection isnull
- If the input collection is a
-
CaseInsensitiveSet
Constructs aCaseInsensitiveSet
containing the elements of the specified collection, using the provided map as the backing implementation.This constructor allows full control over the underlying map implementation, enabling custom behavior for the set.
- Parameters:
source
- the collection whose elements are to be placed into this setbackingMap
- the map to be used as the backing implementation- Throws:
NullPointerException
- if the specified collection or map isnull
-
CaseInsensitiveSet
public CaseInsensitiveSet(int initialCapacity) Constructs an emptyCaseInsensitiveSet
with the specified initial capacity.This constructor is useful for creating a set with a predefined capacity to reduce resizing overhead during population.
- Parameters:
initialCapacity
- the initial capacity of the backing map- Throws:
IllegalArgumentException
- if the specified initial capacity is negative
-
CaseInsensitiveSet
public CaseInsensitiveSet(int initialCapacity, float loadFactor) Constructs an emptyCaseInsensitiveSet
with the specified initial capacity and load factor.This constructor allows fine-grained control over the performance characteristics of the backing map.
- Parameters:
initialCapacity
- the initial capacity of the backing maploadFactor
- the load factor of the backing map, which determines when resizing occurs- Throws:
IllegalArgumentException
- if the specified initial capacity is negative or if the load factor is non-positive
-
-
Method Details
-
hashCode
public int hashCode()For
String
elements, the hash code computation is case-insensitive, as it relies on the case-insensitive hash codes provided by the underlyingCaseInsensitiveMap
. -
equals
For
String
elements, equality is determined in a case-insensitive manner, ensuring that two sets containing equivalent strings with different cases (e.g., "Hello" and "hello") are considered equal. -
size
public int size()Returns the number of elements in this set. For
String
elements, the count is determined in a case-insensitive manner, ensuring that equivalent strings with different cases (e.g., "Hello" and "hello") are counted as a single element. -
isEmpty
public boolean isEmpty()Returns
true
if this set contains no elements. ForString
elements, the check is performed in a case-insensitive manner, ensuring that equivalent strings with different cases are treated as a single element. -
contains
Returns
true
if this set contains the specified element. ForString
elements, the check is performed in a case-insensitive manner, meaning that strings differing only by case (e.g., "Hello" and "hello") are considered equal. -
iterator
Returns an iterator over the elements in this set. For
String
elements, the iterator preserves the original case of the strings, even though the set performs case-insensitive comparisons. -
toArray
Returns an array containing all the elements in this set. For
String
elements, the array preserves the original case of the strings, even though the set performs case-insensitive comparisons. -
toArray
public <T> T[] toArray(T[] a) Returns an array containing all the elements in this set. The runtime type of the returned array is that of the specified array. For
String
elements, the array preserves the original case of the strings, even though the set performs case-insensitive comparisons.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Parameters:
a
- the array into which the elements of the set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose- Returns:
- an array containing all the elements in this set
- Throws:
ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in this setNullPointerException
- if the specified array isnull
-
add
Adds the specified element to this set if it is not already present. For
String
elements, the addition is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are considered equal, and only one instance is added to the set. -
remove
Removes the specified element from this set if it is present. For
String
elements, the removal is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are treated as equal, and removing any of them will remove the corresponding entry from the set. -
containsAll
Returns
true
if this set contains all of the elements in the specified collection. ForString
elements, the comparison is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are treated as equal.- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
- Parameters:
c
- the collection to be checked for containment in this set- Returns:
true
if this set contains all of the elements in the specified collection- Throws:
NullPointerException
- if the specified collection isnull
-
addAll
Adds all the elements in the specified collection to this set if they're not already present. For
String
elements, the addition is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are treated as equal, and only one instance is added to the set.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceSet<E>
- Parameters:
c
- the collection containing elements to be added to this set- Returns:
true
if this set changed as a result of the call- Throws:
NullPointerException
- if the specified collection isnull
or containsnull
elements
-
retainAll
Retains only the elements in this set that are contained in the specified collection. For
String
elements, the comparison is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are treated as equal.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Parameters:
c
- the collection containing elements to be retained in this set- Returns:
true
if this set changed as a result of the call- Throws:
NullPointerException
- if the specified collection isnull
-
removeAll
Removes from this set all of its elements that are contained in the specified collection. For
String
elements, the removal is case-insensitive, meaning that strings differing only by case (e.g., "Hello" and "hello") are treated as equal, and removing any of them will remove the corresponding entry from the set.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceSet<E>
- Parameters:
c
- the collection containing elements to be removed from this set- Returns:
true
if this set changed as a result of the call- Throws:
NullPointerException
- if the specified collection isnull
-
clear
public void clear()Removes all elements from this set. After this call, the set will be empty. For
String
elements, the case-insensitive behavior of the set has no impact on the clearing operation. -
minus
Deprecated. -
minus
Deprecated. -
plus
Deprecated. -
plus
Deprecated. -
toString
Returns a string representation of this set. The string representation consists of a list of the set's elements in their original case, enclosed in square brackets (
"[]"
). ForString
elements, the original case is preserved, even though the set performs case-insensitive comparisons.The order of elements in the string representation matches the iteration order of the backing map.
-