Class SortedList<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>, java.util.Set<T>, java.util.SortedSet<T>

    public class SortedList<T>
    extends java.lang.Object
    implements java.util.SortedSet<T>, java.util.List<T>
    An immutable list that sorts objects by their natural order or through a comparator. It has convenient methods/constructors to create it from collections and iterators. Why not maintain the lists in their sorted form? Well, TreeMaps are quite expensive ... I once profiled bnd and was shocked how much memory the Jar class took due to the TreeMaps. I could not easily change it unfortunately. The other reason is that Parameters uses a LinkedHashMap because the preferred order should be the declaration order. However, sometimes you need to sort the keys by name. Last, and most important reason, is that sometimes you do not know what collection you have or it is not available in a sort ordering (MultiMap for example) ... I found myself sorting these things over and over again and decided to just make an immutable SortedList that is easy to slice and dice
    • Constructor Summary

      Constructors 
      Constructor Description
      SortedList​(java.lang.Comparable<? super T>... x)  
      SortedList​(java.util.Collection<? extends java.lang.Comparable<? super T>> x)  
      SortedList​(java.util.Collection<? extends T> x, int start, int end, java.util.Comparator<? super T> cmp)  
      SortedList​(java.util.Collection<? extends T> x, java.util.Comparator<? super T> cmp)  
      SortedList​(java.util.Comparator<? super T> cmp, T... x)  
      SortedList​(T[] x, int start, int end, java.util.Comparator<? super T> cmp)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void add​(int index, T element)
      Deprecated.
      boolean add​(T e)  
      boolean addAll​(int index, java.util.Collection<? extends T> c)
      Deprecated.
      boolean addAll​(java.util.Collection<? extends T> c)  
      void clear()  
      java.util.Comparator<? super T> comparator()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      static <T> java.util.SortedSet<T> empty()  
      boolean equals​(java.lang.Object other)
      Deprecated.
      T first()  
      static <T extends java.lang.Comparable<? super T>>
      SortedList<T>
      fromIterator​(java.util.Iterator<? extends T> it)  
      static <T> SortedList<T> fromIterator​(java.util.Iterator<? extends T> it, java.util.Comparator<? super T> cmp)  
      T get​(int index)  
      java.lang.Class<?> getType()  
      boolean hasDuplicates()  
      int hashCode()
      Deprecated.
      SortedList<T> headSet​(T toElement)  
      int indexOf​(java.lang.Object o)  
      boolean isEmpty()  
      boolean isEqual​(SortedList<T> list)  
      boolean isSubSet()  
      java.util.Iterator<T> iterator()  
      T last()  
      int lastIndexOf​(java.lang.Object o)  
      java.util.ListIterator<T> listIterator()  
      java.util.ListIterator<T> listIterator​(int index)  
      T remove​(int index)
      Deprecated.
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      T set​(int index, T element)
      Deprecated.
      void setType​(java.lang.Class<?> type)  
      int size()  
      SortedList<T> subList​(int fromIndex, int toIndex)  
      SortedList<T> subSet​(T fromElement, T toElement)  
      java.util.SortedSet<T> tailSet​(T fromElement)  
      java.lang.Object[] toArray()  
      <X> X[] toArray​(X[] a)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        replaceAll, sort, spliterator
      • Methods inherited from interface java.util.SortedSet

        spliterator
    • Constructor Detail

      • SortedList

        public SortedList​(java.util.Collection<? extends java.lang.Comparable<? super T>> x)
      • SortedList

        public SortedList​(java.util.Collection<? extends T> x,
                          java.util.Comparator<? super T> cmp)
      • SortedList

        @SafeVarargs
        public SortedList​(java.lang.Comparable<? super T>... x)
      • SortedList

        public SortedList​(java.util.Comparator<? super T> cmp,
                          T... x)
      • SortedList

        public SortedList​(T[] x,
                          int start,
                          int end,
                          java.util.Comparator<? super T> cmp)
      • SortedList

        public SortedList​(java.util.Collection<? extends T> x,
                          int start,
                          int end,
                          java.util.Comparator<? super T> cmp)
    • Method Detail

      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
        Specified by:
        size in interface java.util.Set<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.List<T>
        Specified by:
        isEmpty in interface java.util.Set<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.List<T>
        Specified by:
        contains in interface java.util.Set<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.List<T>
        Specified by:
        iterator in interface java.util.Set<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
        Specified by:
        toArray in interface java.util.Set<T>
      • toArray

        public <X> X[] toArray​(X[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
        Specified by:
        toArray in interface java.util.Set<T>
      • add

        public boolean add​(T e)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
        Specified by:
        add in interface java.util.Set<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.List<T>
        Specified by:
        remove in interface java.util.Set<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Specified by:
        containsAll in interface java.util.List<T>
        Specified by:
        containsAll in interface java.util.Set<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.Collection<T>
        Specified by:
        addAll in interface java.util.List<T>
        Specified by:
        addAll in interface java.util.Set<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
        Specified by:
        retainAll in interface java.util.List<T>
        Specified by:
        retainAll in interface java.util.Set<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
        Specified by:
        removeAll in interface java.util.List<T>
        Specified by:
        removeAll in interface java.util.Set<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.List<T>
        Specified by:
        clear in interface java.util.Set<T>
      • comparator

        public java.util.Comparator<? super T> comparator()
        Specified by:
        comparator in interface java.util.SortedSet<T>
      • isSubSet

        public boolean isSubSet()
      • subSet

        public SortedList<T> subSet​(T fromElement,
                                    T toElement)
        Specified by:
        subSet in interface java.util.SortedSet<T>
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<T>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<T>
      • tailSet

        public java.util.SortedSet<T> tailSet​(T fromElement)
        Specified by:
        tailSet in interface java.util.SortedSet<T>
      • headSet

        public SortedList<T> headSet​(T toElement)
        Specified by:
        headSet in interface java.util.SortedSet<T>
      • first

        public T first()
        Specified by:
        first in interface java.util.SortedSet<T>
      • last

        public T last()
        Specified by:
        last in interface java.util.SortedSet<T>
      • addAll

        @Deprecated
        public boolean addAll​(int index,
                              java.util.Collection<? extends T> c)
        Deprecated.
        Specified by:
        addAll in interface java.util.List<T>
      • get

        public T get​(int index)
        Specified by:
        get in interface java.util.List<T>
      • set

        @Deprecated
        public T set​(int index,
                     T element)
        Deprecated.
        Specified by:
        set in interface java.util.List<T>
      • add

        @Deprecated
        public void add​(int index,
                        T element)
        Deprecated.
        Specified by:
        add in interface java.util.List<T>
      • remove

        @Deprecated
        public T remove​(int index)
        Deprecated.
        Specified by:
        remove in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator()
        Specified by:
        listIterator in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<T>
      • subList

        public SortedList<T> subList​(int fromIndex,
                                     int toIndex)
        Specified by:
        subList in interface java.util.List<T>
      • equals

        @Deprecated
        public boolean equals​(java.lang.Object other)
        Deprecated.
        Specified by:
        equals in interface java.util.Collection<T>
        Specified by:
        equals in interface java.util.List<T>
        Specified by:
        equals in interface java.util.Set<T>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        @Deprecated
        public int hashCode()
        Deprecated.
        Specified by:
        hashCode in interface java.util.Collection<T>
        Specified by:
        hashCode in interface java.util.List<T>
        Specified by:
        hashCode in interface java.util.Set<T>
        Overrides:
        hashCode in class java.lang.Object
      • isEqual

        public boolean isEqual​(SortedList<T> list)
      • getType

        public java.lang.Class<?> getType()
      • setType

        public void setType​(java.lang.Class<?> type)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hasDuplicates

        public boolean hasDuplicates()
      • fromIterator

        public static <T extends java.lang.Comparable<? super T>> SortedList<T> fromIterator​(java.util.Iterator<? extends T> it)
      • fromIterator

        public static <T> SortedList<T> fromIterator​(java.util.Iterator<? extends T> it,
                                                     java.util.Comparator<? super T> cmp)
      • empty

        public static <T> java.util.SortedSet<T> empty()