Class SortedArrayList<E>
- Type Parameters:
E- The element type.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>,RandomAccess
ArrayList.
Unlike TreeSet, this class allows duplicate elements and maintains them in sorted order
according to a specified Comparator or natural ordering. All modification operations
(add, set, etc.) automatically maintain the sorted order.
This implementation uses an ArrayList internally, providing:
- Fast random access (O(1) for get operations)
- Efficient binary search for finding insertion points (O(log n))
- Array shifting for insertions (O(n))
For better insertion performance with large lists, consider using SortedLinkedList.
Features:
- Maintains elements in sorted order automatically
- Allows duplicate elements (unlike
TreeSet) - Efficient insertion using binary search (O(log n) to find position, O(n) to insert)
- Fast random access (O(1) for get operations)
- Supports custom comparators or natural ordering
Performance Characteristics:
- add(E): O(n) - binary search to find position + array shift
- get(int): O(1) - direct array access
- contains(Object): O(log n) - binary search
- remove(Object): O(n) - binary search + array shift
- set(int, E): O(n) - may need to re-sort if element order changes
Usage:
Notes:
- All elements must be mutually comparable (or comparable via the comparator)
- Null elements are allowed only if the comparator supports them
- The list maintains sorted order at all times - you cannot insert at a specific index
- Calling
set(int, E)may cause the element to move to maintain sorted order - This implementation is not thread-safe
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new sorted list using natural ordering.SortedArrayList(int initialCapacity) Creates a new sorted list with the specified initial capacity using natural ordering.SortedArrayList(Collection<? extends E> c) Creates a new sorted list containing the elements of the specified collection.SortedArrayList(Comparator<? super E> comparator) Creates a new sorted list using the specified comparator.SortedArrayList(Comparator<? super E> comparator, int initialCapacity) Creates a new sorted list with the specified initial capacity and comparator.SortedArrayList(Comparator<? super E> comparator, Collection<? extends E> c) Creates a new sorted list containing the elements of the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position.booleanAdds the specified element to this list in sorted order.booleanaddAll(int index, Collection<? extends E> c) Adds all elements from the specified collection at the specified position.booleanaddAll(Collection<? extends E> c) Adds all elements from the specified collection to this list in sorted order.Comparator<? super E>Returns the comparator used to order the elements in this list.booleanCompares the specified object with this list for equality.get(int index) Returns the element at the specified position.inthashCode()Returns the hash code value for this list.remove(int index) Removes the element at the specified position.Replaces the element at the specified position with the specified element.intsize()Returns the number of elements in this list.toString()Returns a string representation of this list.Methods inherited from class java.util.AbstractList
clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
Constructor Details
-
SortedArrayList
public SortedArrayList()Creates a new sorted list using natural ordering.Elements must implement
Comparable.- Throws:
ClassCastException- if elements are not comparable.
-
SortedArrayList
Creates a new sorted list using the specified comparator.- Parameters:
comparator- The comparator to use for sorting. Must not benull .
-
SortedArrayList
Creates a new sorted list with the specified initial capacity using natural ordering.- Parameters:
initialCapacity- The initial capacity.
-
SortedArrayList
Creates a new sorted list with the specified initial capacity and comparator.- Parameters:
comparator- The comparator to use for sorting. Must not benull .initialCapacity- The initial capacity.
-
SortedArrayList
Creates a new sorted list containing the elements of the specified collection.The elements are sorted according to natural ordering.
- Parameters:
c- The collection whose elements are to be placed into this list.
-
SortedArrayList
Creates a new sorted list containing the elements of the specified collection.- Parameters:
comparator- The comparator to use for sorting. Must not benull .c- The collection whose elements are to be placed into this list.
-
-
Method Details
-
add
Adds the specified element to this list in sorted order.The element is inserted at the appropriate position to maintain sorted order. If there are equal elements, the new element is inserted after existing equal elements (maintains insertion order for equal elements).
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
e- The element to be added.- Returns:
true (as specified byCollection.add(Object)).
-
add
Inserts the specified element at the specified position.Note: The index parameter is ignored - the element is always inserted in sorted order. This method exists to satisfy the
Listinterface contract. -
addAll
Adds all elements from the specified collection to this list in sorted order.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
c- The collection containing elements to be added.- Returns:
true if this list changed as a result of the call.
-
addAll
Adds all elements from the specified collection at the specified position.Note: The index parameter is ignored - elements are always inserted in sorted order.
-
set
Replaces the element at the specified position with the specified element.Important: After setting the element, the list is re-sorted to maintain sorted order. The element may move to a different position if its sort order changed.
- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Parameters:
index- The index of the element to replace.element- The element to be stored at the specified position.- Returns:
- The element previously at the specified position.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
get
Returns the element at the specified position.- Specified by:
getin interfaceList<E>- Specified by:
getin classAbstractList<E>- Parameters:
index- The index of the element to return.- Returns:
- The element at the specified position.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
remove
Removes the element at the specified position.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Parameters:
index- The index of the element to be removed.- Returns:
- The element that was removed.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
size
Returns the number of elements in this list.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- The number of elements in this list.
-
comparator
Returns the comparator used to order the elements in this list.- Returns:
- The comparator, or
null if natural ordering is used.
-
equals
Compares the specified object with this list for equality.Returns
true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. Two elementse1 ande2 are considered equal if(e1==null ? e2==null : e1.equals(e2)) .- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
obj- The object to be compared for equality with this list.- Returns:
true if the specified object is equal to this list.
-
hashCode
Returns the hash code value for this list.The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;for (E e :list )hashCode = 31 *hashCode + (e ==null ? 0 :e .hashCode());- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>- Returns:
- The hash code value for this list.
-
toString
Returns a string representation of this list.The string representation consists of a list of the list's elements in the order they are returned by its iterator, enclosed in square brackets (
"[]" ). Adjacent elements are separated by the characters", " (comma and space). Elements are converted to strings as byString.valueOf(Object) .- Overrides:
toStringin classAbstractCollection<E>- Returns:
- A string representation of this list.
-