Class IdentitySet<T>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<T>
com.cedarsoftware.util.IdentitySet<T>
Type Parameters:
T - the type of elements maintained by this set
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>

public class IdentitySet<T> extends AbstractSet<T>
A lightweight Set implementation that uses object identity (==) instead of equals() for element comparison. Uses open addressing with linear probing for minimal overhead.

Key features:

  • Implements full Set interface
  • Uses object identity (==) not equals() for comparison
  • No Entry objects - single Object[] array
  • Single identityHashCode call per operation
  • Excellent cache locality

This class is a high-performance, drop-in replacement for:


 Set<Object> set = Collections.newSetFromMap(new IdentityHashMap<>());
 

Performance benefits over IdentityHashMap-backed Set:

  • No wrapper layer indirection
  • No Entry object allocations
  • No Boolean.TRUE value storage
  • Better CPU cache utilization (contiguous array)

Thread Safety: This class is not thread-safe. If multiple threads access an IdentitySet concurrently, external synchronization is required.

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

    • IdentitySet

      public IdentitySet()
      Creates a new IdentitySet with default initial capacity (16).
    • IdentitySet

      public IdentitySet(int initialCapacity)
      Creates a new IdentitySet with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity (will be rounded up to power of 2)
    • IdentitySet

      public IdentitySet(int initialCapacity, float loadFactor)
      Creates a new IdentitySet with the specified initial capacity and load factor.
      Parameters:
      initialCapacity - the initial capacity (will be rounded up to power of 2)
      loadFactor - the load factor threshold that triggers resize (must be > 0 and < 1)
    • IdentitySet

      public IdentitySet(Collection<? extends T> c)
      Creates a new IdentitySet containing the elements of the specified collection.
      Parameters:
      c - the collection whose elements are to be placed into this set
      Throws:
      NullPointerException - if the specified collection is null or contains null elements
  • Method Details

    • add

      public boolean add(T element)
      Adds an element to this set using identity comparison.
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Set<T>
      Overrides:
      add in class AbstractCollection<T>
      Parameters:
      element - the element to add (must not be null)
      Returns:
      true if the element was added (was not already present), false otherwise
      Throws:
      NullPointerException - if the element is null
    • contains

      public boolean contains(Object element)
      Checks if this set contains the specified element using identity comparison.
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface Set<T>
      Overrides:
      contains in class AbstractCollection<T>
      Parameters:
      element - the element to check for
      Returns:
      true if the element is present, false otherwise
    • remove

      public boolean remove(Object element)
      Removes the specified element from this set using identity comparison.
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface Set<T>
      Overrides:
      remove in class AbstractCollection<T>
      Parameters:
      element - the element to remove
      Returns:
      true if the element was removed (was present), false otherwise
    • clear

      public void clear()
      Removes all elements from this set.
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface Set<T>
      Overrides:
      clear in class AbstractCollection<T>
    • size

      public int size()
      Returns the number of elements in this set.
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface Set<T>
      Specified by:
      size in class AbstractCollection<T>
      Returns:
      the number of elements
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface Set<T>
      Overrides:
      isEmpty in class AbstractCollection<T>
      Returns:
      true if empty, false otherwise
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements in this set. The elements are returned in no particular order.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Set<T>
      Specified by:
      iterator in class AbstractCollection<T>
      Returns:
      an iterator over the elements in this set