Class CompactSet<E>

  • All Implemented Interfaces:
    Iterable<E>, Collection<E>, Set<E>
    Direct Known Subclasses:
    CompactCIHashSet, CompactCILinkedSet, CompactLinkedSet

    public class CompactSet<E>
    extends AbstractSet<E>
    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.