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
Typically you will create one of the provided subclasses (CompactLinkedSet, CompactCIHashSet, or
CompactCILinkedSet) or extend CompactSet with your own
configuration. The builder pattern is available for advanced cases
when running on a JDK.
CompactLinkedSet<String> set = new CompactLinkedSet<>();
set.add("hello");
// Builder pattern (requires JDK)
CompactSet<String> custom = CompactSet.<String>builder()
.caseSensitive(false)
.sortedOrder()
.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 classBuilder for creating CompactSet instances with custom configurations. -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs an empty CompactSet with the default configuration (i.e., default CompactMap).protectedCompactSet(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 TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) static <E> CompactSet.Builder<E>builder()Returns a builder for creating customized CompactSet instances.voidclear()protected intAllow concrete subclasses to specify the compact size.booleanbooleancontainsAll(Collection<?> c) booleanReturns the configuration settings of this CompactSet.inthashCode()protected booleanAllow concrete subclasses to specify the case-sensitivity.booleanbooleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()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, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods 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 = 50
- unordered
If you want custom config, use the
CompactSet.Builderinstead.- 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:
containsAllin interfaceCollection<E>- Specified by:
containsAllin 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. This API generates subclasses at runtime and therefore requires the JDK compiler tools to be present.- 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. -
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
-