Class ElementAwareArrayList<T>

java.lang.Object
ai.timefold.solver.core.impl.util.ElementAwareArrayList<T>
Type Parameters:
T -

@NullMarked public final class ElementAwareArrayList<T> extends Object
ArrayList-backed list which allows for a cheap removal of an element, while still providing fast iteration and random access. The order of iteration is guaranteed to be the insertion order.

It uses internal state of the entry to track insertion position of the element. When an element is removed, the underlying collection isn't actually touched; therefore, the insertion position of later elements isn't changed. This position is called a gap. Gaps are removed (the list is compacted) when forEach(Consumer) or asList() is called. This keeps the overhead low while giving us all benefits of ArrayList, such as memory efficiency, random access, and fast iteration.

This class is very thread-unsafe.

  • Constructor Details

    • ElementAwareArrayList

      public ElementAwareArrayList()
  • Method Details

    • add

      public ElementAwareArrayList.Entry<T> add(T element)
      Appends the specified element to the end of this collection.
      Parameters:
      element - element to be appended to this collection
    • get

      public ElementAwareArrayList.Entry<T> get(int index)
    • remove

      public void remove(ElementAwareArrayList.Entry<T> entry)
      Removes the first occurrence of the specified element from this collection, if present.
      Parameters:
      entry - entry to be removed from this collection
      Throws:
      IllegalStateException - if the element wasn't found in this collection
    • isEmpty

      public boolean isEmpty()
    • size

      public int size()
    • forEach

      public void forEach(Consumer<T> elementConsumer)
      Performs the given action for each element of the collection until all elements have been processed.
      Parameters:
      elementConsumer - the action to be performed for each element; mustn't modify the collection and mustn't throw exceptions, as that'd leave the collection in an inconsistent state
    • iterator

      public Iterator<T> iterator()
    • asList

      public List<ElementAwareArrayList.Entry<T>> asList()
      Returns a standard List view of this collection. Users mustn't modify the returned list, as that'd also change the underlying data structure.
      Returns:
      a standard list view of this element-aware list