Class PriorityList<E extends Prioritizable>

java.lang.Object
studio.mevera.imperat.util.priority.PriorityList<E>
Type Parameters:
E - the type of elements in this list
All Implemented Interfaces:
Iterable<E>

public class PriorityList<E extends Prioritizable> extends Object implements Iterable<E>
A collection that maintains elements sorted by their Priority. Elements with higher priority values are ordered first.

This implementation uses a TreeMap with a composite key to maintain sorted order automatically when elements are added. Iteration is O(n) without additional sorting overhead.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty PriorityList.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(E element)
    Adds an element with the specified priority.
    @NotNull PriorityList<E>
    Returns an immutable view of this PriorityList.
    void
    Removes all elements from this list.
    boolean
    contains(E element)
    Checks if this list contains the specified element.
    void
    forEach(Consumer<? super E> action)
    Performs the given action for each element in priority order.
    boolean
    Checks if this list is empty.
    @NotNull Iterator<E>
    Returns an iterator over the elements in this list, ordered by priority.
    boolean
    remove(E element)
    Removes an element from this list.
    int
    Returns the number of elements in this list.
    Returns a sequential Stream of the elements in this list, ordered by priority.
    Returns a list of all elements in priority order.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    spliterator
  • Constructor Details

    • PriorityList

      public PriorityList()
      Creates a new empty PriorityList.
  • Method Details

    • add

      public boolean add(E element)
      Adds an element with the specified priority. If the element already exists, it will not be added again.
      Parameters:
      element - the element to add
      Returns:
      true if the element was added, false if it already existed
    • remove

      public boolean remove(E element)
      Removes an element from this list.
      Parameters:
      element - the element to remove
      Returns:
      true if the element was removed, false if it wasn't present
    • contains

      public boolean contains(E element)
      Checks if this list contains the specified element.
      Parameters:
      element - the element to check for
      Returns:
      true if the element is present
    • size

      public int size()
      Returns the number of elements in this list.
      Returns:
      the size of this list
    • isEmpty

      public boolean isEmpty()
      Checks if this list is empty.
      Returns:
      true if this list contains no elements
    • clear

      public void clear()
      Removes all elements from this list.
    • iterator

      @NotNull public @NotNull Iterator<E> iterator()
      Returns an iterator over the elements in this list, ordered by priority. Elements with higher priority are returned first. The TreeMap maintains sorted order, so no additional sorting is needed.
      Specified by:
      iterator in interface Iterable<E extends Prioritizable>
      Returns:
      an iterator over the elements
    • forEach

      public void forEach(Consumer<? super E> action)
      Performs the given action for each element in priority order.
      Specified by:
      forEach in interface Iterable<E extends Prioritizable>
      Parameters:
      action - the action to perform
    • toList

      public List<E> toList()
      Returns a list of all elements in priority order.
      Returns:
      a new list containing all elements
    • stream

      public Stream<E> stream()
      Returns a sequential Stream of the elements in this list, ordered by priority. Elements with higher priority are returned first.
      Returns:
      a sequential stream of elements in priority order
    • asUnmodifiable

      @NotNull public @NotNull PriorityList<E> asUnmodifiable()
      Returns an immutable view of this PriorityList. The returned list will reflect any changes made to the underlying list, but mutation operations will throw UnsupportedOperationException.
      Returns:
      an immutable view of this PriorityList
    • toString

      public String toString()
      Overrides:
      toString in class Object