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
Allow concrete subclasses to specify the compact size.boolean
boolean
containsAll
(Collection<?> c) boolean
Returns the configuration settings of this CompactSet.Allow concrete subclasses to specify the internal set to use when larger than compactSize.int
hashCode()
protected boolean
Allow concrete subclasses to specify the case-sensitivity.boolean
boolean
isEmpty()
iterator()
boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
withConfig
(Map<String, Object> config) 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
-
isDefaultCompactSet
public boolean isDefaultCompactSet() -
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
protected int compactSize()Allow concrete subclasses to specify the compact size. Concrete subclasses are useful to simplify serialization. -
isCaseInsensitive
protected boolean isCaseInsensitive()Allow concrete subclasses to specify the case-sensitivity. Concrete subclasses are useful to simplify serialization. -
getNewSet
Allow concrete subclasses to specify the internal set to use when larger than compactSize. Concrete subclasses are useful to simplify serialization. -
getConfig
Returns the configuration settings of this CompactSet.The returned map contains the following keys:
CompactMap.COMPACT_SIZE
- Maximum size before switching to backing mapCompactMap.CASE_SENSITIVE
- Whether string elements are case-sensitiveCompactMap.ORDERING
- Element ordering strategy
- Returns:
- an unmodifiable map containing the configuration settings
-
withConfig
-