Class SortedList<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Deque<T>, java.util.List<T>, java.util.Queue<T>

    public class SortedList<T>
    extends java.util.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:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean ascending
      The ordering direction of the list.
      protected java.util.Comparator<? super T> comp
      The comparator to use (if any).
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      SortedList()
      Constructs an empty list with ascending order.
      SortedList​(boolean ascending)
      Constructs an empty list with given order.
      SortedList​(java.util.Comparator<? super T> comp, boolean ascending)
      Constructs an empty list with given order.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, T element)
      Inserts the specified element at the specified position in this list.
      boolean add​(T o)
      Appends the specified element to the end of this list.
      boolean addAll​(int index, java.util.Collection<? extends T> c)
      Inserts all of the elements in the specified collection into this list, starting at the specified position.
      boolean addAll​(java.util.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.
      void addFirst​(T o)
      Inserts the given element at the beginning of this list.
      void addLast​(T o)
      Appends the given element to the end of this list.
      protected int compare​(T o1, T o2)
      Compare two elements.
      int insertElement​(int index, T obj)
      Insert an element into the list.
      • Methods inherited from class java.util.LinkedList

        clear, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, set, size, spliterator, toArray, toArray
      • Methods inherited from class java.util.AbstractSequentialList

        iterator
      • Methods inherited from class java.util.AbstractList

        equals, hashCode, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        containsAll, isEmpty, removeAll, retainAll, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.util.Deque

        iterator
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
    • Field Detail

      • ascending

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

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

      • 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​(java.util.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 Detail

      • addFirst

        public void addFirst​(T o)
        Inserts the given element at the beginning of this list.
        Specified by:
        addFirst in interface java.util.Deque<T>
        Overrides:
        addFirst in class java.util.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
        Specified by:
        addLast in interface java.util.Deque<T>
        Overrides:
        addLast in class java.util.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 java.util.Collection<T>
        Specified by:
        add in interface java.util.Deque<T>
        Specified by:
        add in interface java.util.List<T>
        Specified by:
        add in interface java.util.Queue<T>
        Overrides:
        add in class java.util.LinkedList<T>
        Parameters:
        o - element to be appended to this list.
        Returns:
      • addAll

        public boolean addAll​(java.util.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 java.util.Collection<T>
        Specified by:
        addAll in interface java.util.Deque<T>
        Specified by:
        addAll in interface java.util.List<T>
        Overrides:
        addAll in class java.util.LinkedList<T>
        Parameters:
        c - the elements to be inserted into this list.
        Returns:
        Throws:
        java.lang.NullPointerException - if the specified collection is null.
      • addAll

        public boolean addAll​(int index,
                              java.util.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 java.util.List<T>
        Overrides:
        addAll in class java.util.LinkedList<T>
        Parameters:
        index - index at which to insert first element from the specified collection.
        c - elements to be inserted into this list.
        Returns:
        Throws:
        java.lang.IndexOutOfBoundsException - if the specified index is out of range (
        java.lang.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 java.util.List<T>
        Overrides:
        add in class java.util.LinkedList<T>
        Parameters:
        index - index at which the specified element is to be inserted.
        element - element to be inserted.
        Throws:
        java.lang.IndexOutOfBoundsException - if the specified index is out of range (
      • 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.