Class SortedList<T>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<T>, Collection<T>, Deque<T>, List<T>, Queue<T>, SequencedCollection<T>

public class SortedList<T> extends LinkedList<T>
A sorted list allowing duplicates of elements (unlike java.util.TreeSet). The list is kept sorted, while elements are being added.
See Also:
  • Field Details

    • ascending

      protected boolean ascending
      The ordering direction of the list.
    • comp

      protected Comparator<? super T> comp
      The comparator to use (if any).
  • Constructor Details

    • SortedList

      public SortedList()
      Constructs an empty list with ascending order.
    • SortedList

      public SortedList(boolean ascending)
      Constructs an empty list with given order.
      Parameters:
      ascending - True, if the list should sort ascending.
    • SortedList

      public SortedList(Comparator<? super T> comp, boolean ascending)
      Constructs an empty list with given order.
      Parameters:
      comp - A comparator to use for comparing elements.
      ascending - True, if the list shoudl sort ascending.
  • Method Details

    • addFirst

      public void addFirst(T o)
      Inserts the given element at the beginning of this list.
      Specified by:
      addFirst in interface Deque<T>
      Specified by:
      addFirst in interface List<T>
      Specified by:
      addFirst in interface SequencedCollection<T>
      Overrides:
      addFirst in class LinkedList<T>
      Parameters:
      o - the element to be inserted at the beginning of this list.
    • addLast

      public void addLast(T o)
      Appends the given element to the end of this list. (Identical in function to the add method; included only for consistency.)
      Specified by:
      addLast in interface Deque<T>
      Specified by:
      addLast in interface List<T>
      Specified by:
      addLast in interface SequencedCollection<T>
      Overrides:
      addLast in class LinkedList<T>
      Parameters:
      o - the element to be inserted at the end of this list.
    • add

      public boolean add(T o)
      Appends the specified element to the end of this list.
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Deque<T>
      Specified by:
      add in interface List<T>
      Specified by:
      add in interface Queue<T>
      Overrides:
      add in class LinkedList<T>
      Parameters:
      o - element to be appended to this list.
      Returns:
      true (as per the general contract of Collection.add).
    • addAll

      public boolean addAll(Collection<? extends T> c)
      Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface Deque<T>
      Specified by:
      addAll in interface List<T>
      Overrides:
      addAll in class LinkedList<T>
      Parameters:
      c - the elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      NullPointerException - if the specified collection is null.
    • addAll

      public boolean addAll(int index, Collection<? extends T> c)
      Inserts all of the elements in the specified collection into this list, starting at the specified position.
      Specified by:
      addAll in interface List<T>
      Overrides:
      addAll in class LinkedList<T>
      Parameters:
      index - index at which to insert first element from the specified collection.
      c - elements to be inserted into this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).
      NullPointerException - if the specified collection is null.
    • add

      public void add(int index, T element)
      Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
      Specified by:
      add in interface List<T>
      Overrides:
      add in class LinkedList<T>
      Parameters:
      index - index at which the specified element is to be inserted.
      element - element to be inserted.
      Throws:
      IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).
    • insertElement

      public int insertElement(int index, T obj)
      Insert an element into the list.
      Parameters:
      index - The index where to start insertion.
      obj - The element to insert.
      Returns:
      The index where the element was actually inserted.
    • compare

      protected int compare(T o1, T o2)
      Compare two elements.