Class ReversedList<E>
- Type Parameters:
E- The element type.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>,RandomAccess
This class provides a read-only reversed view of a list, where element access is transparently reversed without copying or modifying the original list. All read operations (get, iterator, etc.) operate on the underlying list in reverse order.
Features:
- Zero-copy reverse view - no data duplication
- Efficient random access via index translation
- Reflects changes in the underlying list automatically
- Read-only - modification operations throw
UnsupportedOperationException - Iterator and ListIterator support in reversed order
Usage:
Notes:
- The underlying list must not be null
- Changes to the underlying list are immediately visible in the reversed view
- All modification operations (add, remove, set, clear) throw
UnsupportedOperationException - Size changes in the underlying list are reflected in this view
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionReversedList(List<E> list) Creates a new reversed view of the specified list. -
Method Summary
Modifier and TypeMethodDescriptionvoidNot supported - this is a read-only view.voidclear()Not supported - this is a read-only view.booleanCompares the specified object with this list for equality.get(int index) Returns the element at the specified position in this reversed view.inthashCode()Returns the hash code value for this list.iterator()Returns an iterator over the elements in this reversed view in proper sequence.Returns a list iterator over the elements in this reversed view.listIterator(int index) Returns a list iterator over the elements in this reversed view, starting at the specified position.remove(int index) Not supported - this is a read-only view.Not supported - this is a read-only view.intsize()Returns the number of elements in this reversed view.subList(int fromIndex, int toIndex) Returns a view of the portion of this reversed list between the specified indices.toString()Returns a string representation of this reversed list.Methods inherited from class java.util.AbstractList
add, addAll, indexOf, lastIndexOf, removeRangeMethods inherited from class java.util.AbstractCollection
addAll, 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
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
Constructor Details
-
ReversedList
Creates a new reversed view of the specified list.- Parameters:
list- The list to reverse. Must not benull .- Throws:
IllegalArgumentException- if list isnull .
-
-
Method Details
-
add
Not supported - this is a read-only view.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
clear
Not supported - this is a read-only view.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
get
Returns the element at the specified position in this reversed view.The position is translated to access the underlying list in reverse order. For example, index 0 returns the last element of the underlying list.
- Specified by:
getin interfaceList<E>- Specified by:
getin classAbstractList<E>- Parameters:
index- The index of the element to return (0-based, in reversed order).- Returns:
- The element at the specified position in the reversed view.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
iterator
Returns an iterator over the elements in this reversed view in proper sequence.The iterator traverses the underlying list in reverse order.
-
listIterator
Returns a list iterator over the elements in this reversed view.The iterator traverses the underlying list in reverse order.
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Returns:
- A list iterator over the elements in reversed order.
-
listIterator
Returns a list iterator over the elements in this reversed view, starting at the specified position.The iterator traverses the underlying list in reverse order, starting from the translated position.
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Parameters:
index- The index of the first element to be returned from the list iterator (in reversed order).- Returns:
- A list iterator over the elements in reversed order.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
remove
Not supported - this is a read-only view.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
set
Not supported - this is a read-only view.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
size
Returns the number of elements in this reversed view.This is always equal to the size of the underlying list.
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- The number of elements in this reversed view.
-
subList
Returns a view of the portion of this reversed list between the specified indices.The returned sublist is also a reversed view and reflects changes in the underlying list.
- Specified by:
subListin interfaceList<E>- Overrides:
subListin classAbstractList<E>- Parameters:
fromIndex- Low endpoint (inclusive) of the subList.toIndex- High endpoint (exclusive) of the subList.- Returns:
- A view of the specified range within this reversed list.
- Throws:
IndexOutOfBoundsException- if the indices are out of range.
-
toString
Returns a string representation of this reversed list.The format follows the standard Java list convention:
"[element1, element2, ...]" Elements are shown in reversed order (as they appear in this view).- Overrides:
toStringin classAbstractCollection<E>- Returns:
- A string representation of this reversed list.
-
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. In other words, two lists are defined to be equal if they contain the same elements in the same order.This implementation compares elements in the reversed order as they appear in this view.
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
o- 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());This ensures that
list1.equals(list2) implies thatlist1.hashCode()==list2.hashCode() for any two listslist1 andlist2 , as required by the general contract ofObject.hashCode().This implementation computes the hash code from the elements in reversed order as they appear in this view.
- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>- Returns:
- The hash code value for this list.
-