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

public class CompactSet<E> extends Object implements Set<E>
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.
  • 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 - if compactSize() returns a value less than 2
    • CompactSet

      protected CompactSet(CompactMap<E,Object> map)
      Constructs a CompactSet with a pre-existing CompactMap (usually from a builder).
      Parameters:
      map - the underlying CompactMap to store elements
    • CompactSet

      public CompactSet(Collection<? extends E> c)
      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()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface Set<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface Set<E>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface Set<E>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static <E> CompactSet.Builder<E> 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

      protected Set<E> getNewSet()
      Allow concrete subclasses to specify the internal set to use when larger than compactSize. Concrete subclasses are useful to simplify serialization.
    • getConfig

      public Map<String,Object> getConfig()
      Returns the configuration settings of this CompactSet.

      The returned map contains the following keys:

      Returns:
      an unmodifiable map containing the configuration settings
    • withConfig

      public CompactSet<E> withConfig(Map<String,Object> config)