Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the SortedList implementation I am using. Maybe this helps with your problem:</p> <pre><code>import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; /** * This class is a List implementation which sorts the elements using the * comparator specified when constructing a new instance. * * @param &lt;T&gt; */ public class SortedList&lt;T&gt; extends ArrayList&lt;T&gt; { /** * Needed for serialization. */ private static final long serialVersionUID = 1L; /** * Comparator used to sort the list. */ private Comparator&lt;? super T&gt; comparator = null; /** * Construct a new instance with the list elements sorted in their * {@link java.lang.Comparable} natural ordering. */ public SortedList() { } /** * Construct a new instance using the given comparator. * * @param comparator */ public SortedList(Comparator&lt;? super T&gt; comparator) { this.comparator = comparator; } /** * Construct a new instance containing the elements of the specified * collection with the list elements sorted in their * {@link java.lang.Comparable} natural ordering. * * @param collection */ public SortedList(Collection&lt;? extends T&gt; collection) { addAll(collection); } /** * Construct a new instance containing the elements of the specified * collection with the list elements sorted using the given comparator. * * @param collection * @param comparator */ public SortedList(Collection&lt;? extends T&gt; collection, Comparator&lt;? super T&gt; comparator) { this(comparator); addAll(collection); } /** * Add a new entry to the list. The insertion point is calculated using the * comparator. * * @param paramT * @return &lt;code&gt;true&lt;/code&gt; if this collection changed as a result of the call. */ @Override public boolean add(T paramT) { int initialSize = this.size(); // Retrieves the position of an existing, equal element or the // insertion position for new elements (negative). int insertionPoint = Collections.binarySearch(this, paramT, comparator); super.add((insertionPoint &gt; -1) ? insertionPoint : (-insertionPoint) - 1, paramT); return (this.size() != initialSize); } /** * Adds all elements in the specified collection to the list. Each element * will be inserted at the correct position to keep the list sorted. * * @param paramCollection * @return &lt;code&gt;true&lt;/code&gt; if this collection changed as a result of the call. */ @Override public boolean addAll(Collection&lt;? extends T&gt; paramCollection) { boolean result = false; if (paramCollection.size() &gt; 4) { result = super.addAll(paramCollection); Collections.sort(this, comparator); } else { for (T paramT:paramCollection) { result |= add(paramT); } } return result; } /** * Check, if this list contains the given Element. This is faster than the * {@link #contains(Object)} method, since it is based on binary search. * * @param paramT * @return &lt;code&gt;true&lt;/code&gt;, if the element is contained in this list; * &lt;code&gt;false&lt;/code&gt;, otherwise. */ public boolean containsElement(T paramT) { return (Collections.binarySearch(this, paramT, comparator) &gt; -1); } /** * @return The comparator used for sorting this list. */ public Comparator&lt;? super T&gt; getComparator() { return comparator; } /** * Assign a new comparator and sort the list using this new comparator. * * @param comparator */ public void setComparator(Comparator&lt;? super T&gt; comparator) { this.comparator = comparator; Collections.sort(this, comparator); } } </code></pre> <p>This solution is very flexible and uses existing Java functions:</p> <ul> <li>Completely based on generics</li> <li>Uses java.util.Collections for finding and inserting list elements</li> <li>Option to use a custom Comparator for list sorting</li> </ul> <p>Some notes:</p> <ul> <li>This sorted list is <strong>not synchronized</strong> since it inherits from <code>java.util.ArrayList</code>. Use <code>Collections.synchronizedList</code> if you need this (refer to the Java documentation for <code>java.util.ArrayList</code> for details).</li> <li>The initial solution was based on <code>java.util.LinkedList</code>. For better performance, specifically for finding the insertion point (Logan's comment) and quicker <strong>get</strong> operations (<a href="https://dzone.com/articles/arraylist-vs-linkedlist-vs" rel="nofollow noreferrer">https://dzone.com/articles/arraylist-vs-linkedlist-vs</a>), this has been changed to <code>java.util.ArrayList</code>.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload