Class WeakIdentitySet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.litebridge.commons.type.WeakIdentitySet<E>
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

public final class WeakIdentitySet<E> extends AbstractSet<E>
A Set implementation that uses identity comparison for object equality, and holds its elements weakly, meaning elements are eligible for garbage collection if no strong references to them exist outside this set.

This class is backed by a WeakIdentityMap with the keys representing the elements of the set and values serving as placeholders.

  • Constructor Details

    • WeakIdentitySet

      public WeakIdentitySet()
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements of the set, allowing traversal of the elements while cleaning up any stale entries. This iterator supports the remove() operation to remove the currently returned element from the set, but makes no guarantees about the order in which elements are returned.

      The iterator ensures that only non-stale elements, whose keys are still strongly reachable, are available for iteration. Stale entries whose references have been garbage-collected are removed as part of the iteration process.

      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
      Returns:
      an Iterator<K> over the elements of the set
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>