Class FilteredStringSet

java.lang.Object
com.github.tommyettinger.ds.ObjectSet<String>
com.github.tommyettinger.ds.FilteredStringSet
All Implemented Interfaces:
EnhancedCollection<String>, Iterable<String>, Collection<String>, Set<String>

public class FilteredStringSet extends ObjectSet<String>
A customizable variant on ObjectSet that always uses String keys, but only considers any character in an item (for equality and hashing purposes) if that character satisfies a predicate. This can also edit the characters that pass the filter, such as by changing their case during comparisons (and hashing). You will usually want to call setFilter(CharFilter) to change the behavior of hashing and equality before you enter any items, unless you have specified the CharFilter you want in the constructor.
You can use this class as a replacement for CaseInsensitiveSet if you set the editor to a method reference to Character.toUpperCase(char) or Casing.caseUp(char). You can go further by setting the filter to make the hashing and equality checks ignore characters that don't satisfy a predicate, such as Character.isLetter(char). CaseInsensitiveSet does allow taking arbitrary CharSequence types as keys, but it doesn't permit modifying them, so usually Strings are a good choice anyway.
Be advised that if you use some (most) checks in Character for properties of a char, and you try to use them on GWT, those checks will not work as expected for non-ASCII characters. Some other platforms might also be affected, such as TeaVM, but it isn't clear yet which platforms have full Unicode support. You can consider depending upon RegExodus for more cross-platform Unicode support; a method reference to Category.L::contains acts like Character::isLetter, but works on GWT. com.github.tommyettinger.ds.support.util.CharPredicates provides a few common CharPredicate constants that will work identically on all platforms.
  • Field Details

  • Constructor Details

    • FilteredStringSet

      public FilteredStringSet()
      Creates a new set with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor(). This considers all characters in a String key and does not edit them.
    • FilteredStringSet

      public FilteredStringSet(int initialCapacity)
      Creates a new set with the specified initial capacity a load factor of Utilities.getDefaultLoadFactor(). This set will hold initialCapacity items before growing the backing table. This considers all characters in a String key and does not edit them.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • FilteredStringSet

      public FilteredStringSet(int initialCapacity, float loadFactor)
      Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table. This considers all characters in a String key and does not edit them.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter)
      Creates a new set with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor(). This uses the specified CharFilter.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter, int initialCapacity)
      Creates a new set with the specified initial capacity and the default load factor. This set will hold initialCapacity items before growing the backing table. This uses the specified CharFilter.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter, int initialCapacity, float loadFactor)
      Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table. This uses the specified CharFilter.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • FilteredStringSet

      public FilteredStringSet(FilteredStringSet set)
      Creates a new set identical to the specified set.
      Parameters:
      set - another FilteredStringSet to copy
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter, Collection<? extends String> coll)
      Creates a new set that contains all distinct elements in coll. This uses the specified CharFilter, including while it enters the items in coll.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
      coll - a Collection implementation to copy, such as an ObjectList or a Set that isn't a FilteredStringSet
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter, String[] array, int offset, int length)
      Creates a new set using length items from the given array, starting at offset (inclusive). This uses the specified CharFilter, including while it enters the items in array.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
      array - an array to draw items from
      offset - the first index in array to draw an item from
      length - how many items to take from array; bounds-checking is the responsibility of the using code
    • FilteredStringSet

      public FilteredStringSet(CharFilter filter, String[] array)
      Creates a new set containing all the items in the given array. This uses the specified CharFilter, including while it enters the items in array.
      Parameters:
      filter - a CharFilter that can be obtained with CharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
      array - an array that will be used in full, except for duplicate items
  • Method Details