Package com.cedarsoftware.util
Class CompactSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
com.cedarsoftware.util.CompactSet<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
- Direct Known Subclasses:
CompactCIHashSet,CompactCILinkedSet,CompactLinkedSet
Often, memory may be consumed by lots of Maps or Sets (HashSet uses a HashMap to implement it's set). HashMaps
and other similar Maps often have a lot of blank entries in their internal structures. If you have a lot of Maps
in memory, perhaps representing JSON objects, large amounts of memory can be consumed by these empty Map entries.
CompactSet is a Set that strives to reduce memory at all costs while retaining speed that is close to HashSet's speed.
It does this by using only one (1) member variable (of type Object) and changing it as the Set grows. It goes from
an Object[] to a Set when the size() of the Set crosses the threshold defined by the method compactSize() (defaults
to 80). After the Set crosses compactSize() size, then it uses a Set (defined by the user) to hold the items. This
Set is defined by a method that can be overridden, which returns a new empty Set() for use in the > compactSize()
state.
Methods you may want to override:
// Map you would like it to use when size() > compactSize(). HashSet is default
protected abstract Map<K, V> getNewMap();
// If you want case insensitivity, return true and return new CaseInsensitiveSet or TreeSet(String.CASE_INSENSITIVE_PRDER) from getNewSet()
protected boolean isCaseInsensitive() { return false; }
// When size() > than this amount, the Set returned from getNewSet() is used to store elements.
protected int compactSize() { return 80; }
This Set supports holding a null element.- 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
http://www.apache.org/licenses/LICENSE-2.0
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.
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringMethods 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
addAll, containsAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
CompactSet
public CompactSet() -
CompactSet
-
-
Method Details
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
iterator
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
getNewSet
- Returns:
- new empty Set instance to use when size() becomes > compactSize().
-
getNewSet
-
isCaseInsensitive
protected boolean isCaseInsensitive() -
compactSize
protected int compactSize()
-