Package com.cedarsoftware.util
Class CompactSet<E>
java.lang.Object
com.cedarsoftware.util.CompactSet<E>
- Type Parameters:
E
- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
- Direct Known Subclasses:
CompactCIHashSet
,CompactCILinkedSet
,CompactLinkedSet
A memory-efficient Set implementation that internally uses
CompactMap
.
This implementation provides the same memory benefits as CompactMap while maintaining proper Set semantics. It can be configured for:
- Case sensitivity for String elements
- Element ordering (sorted, reverse, insertion)
- Custom compact size threshold
Creating a CompactSet
// Create a case-insensitive, sorted CompactSet
CompactSet<String> set = CompactSet.<String>builder()
.caseSensitive(false)
.sortedOrder()
.compactSize(80)
.build();
// Create a CompactSet with insertion ordering
CompactSet<String> ordered = CompactSet.<String>builder()
.insertionOrder()
.build();
- 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Builder for creating CompactSet instances with custom configurations. -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs an empty CompactSet with the default configuration (i.e., default CompactMap).protected
CompactSet
(CompactMap<E, Object> map) Constructs a CompactSet with a pre-existing CompactMap (usually from a builder).CompactSet
(Collection<? extends E> c) Constructs a CompactSet containing the elements of the specified collection, using the default CompactMap configuration. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) static <E> CompactSet.Builder
<E> builder()
Returns a builder for creating customized CompactSet instances.void
clear()
protected int
Deprecated.boolean
boolean
containsAll
(Collection<?> c) boolean
Deprecated.Legacy method.int
hashCode()
protected boolean
Deprecated.UseCompactSet.Builder.caseSensitive(boolean)
instead.boolean
isEmpty()
iterator()
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
-
CompactSet
public CompactSet()Constructs an empty CompactSet with the default configuration (i.e., default CompactMap).This uses the no-arg CompactMap constructor, which typically yields:
- caseSensitive = true
- compactSize = 70
- unordered
If you want custom config, use the
CompactSet.Builder
instead.- Throws:
IllegalStateException
- ifcompactSize()
returns a value less than 2
-
CompactSet
Constructs a CompactSet with a pre-existing CompactMap (usually from a builder).- Parameters:
map
- the underlying CompactMap to store elements
-
CompactSet
Constructs a CompactSet containing the elements of the specified collection, using the default CompactMap configuration.- Parameters:
c
- the collection whose elements are to be placed into this set- Throws:
NullPointerException
- if the specified collection is null
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
add
-
remove
-
clear
public void clear() -
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
-
addAll
-
retainAll
-
removeAll
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
equals
-
hashCode
public int hashCode() -
toString
-
builder
Returns a builder for creating customized CompactSet instances.- Type Parameters:
E
- the type of elements in the set- Returns:
- a new Builder instance
-
compactSize
Deprecated.UseCompactSet.Builder.compactSize(int)
instead. Maintained for backward compatibility with existing subclasses. -
isCaseInsensitive
Deprecated.UseCompactSet.Builder.caseSensitive(boolean)
instead. Maintained for backward compatibility with existing subclasses. -
getNewSet
Deprecated.Legacy method. Subclasses should configure CompactSet using the builder pattern instead. Maintained for backward compatibility with existing subclasses.
-
CompactSet.Builder.compactSize(int)
instead.