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
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.
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, 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
addAll, containsAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
CompactSet
public CompactSet() -
CompactSet
-
-
Method Details
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
iterator
-
add
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
getNewSet
- Returns:
- new empty Set instance to use when size() becomes > compactSize().
-
getNewSet
-
isCaseInsensitive
protected boolean isCaseInsensitive() -
compactSize
protected int compactSize()
-