Class ObjectDeque<T>
- All Implemented Interfaces:
Arrangeable,Arrangeable.ArrangeableList<T>,EnhancedCollection<T>,Lisque<T>,Iterable<T>,Collection<T>,Deque<T>,List<T>,Queue<T>,RandomAccess
List and Deque interfaces, and supports RandomAccess.
Values in the backing array may wrap back to the beginning, making add and remove at the beginning and end O(1)
(unless the backing array needs to resize when adding). Deque functionality is provided via removeLast() and
addFirst(Object).
Unlike most Deque implementations in the JDK, you can get and set items anywhere in the deque in constant time with
get(int) and set(int, Object). Relative to an ObjectList, get(int) has slightly
higher overhead, but it still runs in constant time. Unlike ArrayDeque in the JDK, this implements
equals(Object) and hashCode(), as well as equalsIdentity(Object). This can provide
full-blown ListIterators for iteration from an index or in reverse order.
Unlike
ArrayDeque or ArrayList, most methods that take an index here try to be "forgiving;" that is,
they treat negative indices as index 0, and too-large indices as the last index, rather than throwing an Exception,
except in some cases where the ObjectDeque is empty and an item from it is required. An exception is in
set(int, Object), which allows prepending by setting a negative index, or appending by setting a too-large
index. This isn't a standard JDK behavior, and it doesn't always act how Deque or List is documented.
Some new methods are present here, or have been made public when they weren't before.
removeRange(int, int),
for instance, is now public, as is resize(int). New APIs include Deque-like methods that affect the middle
of the deque, such as peekAt(int) and pollAt(int). There are more bulk methods that work at the
head or tail region of the deque, such as addAllFirst(Collection) and truncateFirst(int). There are
the methods from Arrangeable, and relevant ones from Ordered (this isn't Ordered because it doesn't
provide its order as an ObjectList, but can do similar things).
In general, this is an improvement over
ArrayDeque in every type of functionality, and is mostly equivalent
to ObjectList as long as the performance of get(int) is adequate. Because it is array-backed, it
should usually be much faster than LinkedList, as well; only periodic resizing and modifications in the
middle of the List using an iterator should be typically faster for LinkedList.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classNested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T> -
Field Summary
FieldsModifier and TypeFieldDescriptionThe value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when callingpeek()on an empty deque.protected ObjectDeque.ObjectDequeIterator<T>protected ObjectDeque.ObjectDequeIterator<T>protected intIndex of first element.protected T[]Contains the values in the deque.protected ObjectDeque.ObjectDequeIterator<T>protected ObjectDeque.ObjectDequeIterator<T>intNumber of elements in the deque.protected intIndex of last element.Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new ObjectDeque which can hold 16 values without needing to resize the backing array.ObjectDeque(int initialSize) Creates a new ObjectDeque which can hold the specified number of values without needing to resize the backing array.ObjectDeque(ObjectDeque<? extends T> deque) Copies the given ObjectDeque exactly into this one.ObjectDeque(Collection<? extends T> coll) Creates a new ObjectDeque using all the contents of the given Collection.ObjectDeque(Iterator<? extends T> iter) Creates a new instance containing the items in the specified iterator.ObjectDeque(T[] a) Creates a new ObjectDeque using all the contents of the given array.ObjectDeque(T[] a, int offset, int count) Creates a new ObjectDeque usingcountitems froma, starting atoffset. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element into this deque at the specified index.booleanInserts the specified element into the deque represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success and throwing anIllegalStateExceptionif no space is currently available.booleanAdds all parameters usingCollection.add(Object)for each one.booleanAdds all parameters usingCollection.add(Object)for each one.booleanAdds all parameters usingCollection.add(Object)for each one.booleanaddAll(int index, Collection<? extends T> c) booleanLikeaddAll(int, Collection), but takes an array instead of a Collection and inserts it so the first item will be at the givenindex.booleanLikeaddAll(int, Collection), but takes an array instead of a Collection, gets items starting atoffsetfrom that array, usinglengthitems, and inserts them so the item at the given offset will be at the givenindex.booleanaddAll(Collection<? extends T> c) Adds all the elements in the specified collection at the end of this deque, as if by callingaddLast(T)on each one, in the order that they are returned by the collection's iterator.booleanExactly likeaddAll(Collection), but takes an array instead of a Collection.booleanbooleanaddAllFirst(Collection<? extends T> c) Adds every item incto this in order at the start.booleanaddAllFirst(T[] array) Exactly likeaddAllFirst(Collection), but takes an array instead of a Collection.booleanaddAllFirst(T[] array, int offset, int length) booleanaddAllLast(Collection<? extends T> c) An alias foraddAll(Collection), this adds every item incto this in order at the end.booleanaddAllLast(T[] array) An alias foraddAll(Object[]).booleanaddAllLast(T[] array, int offset, int length) An alias foraddAll(Object[], int, int).voidPrependsvalueto the head (enqueue to head).voidvoidvoidvoidAppendsvalueto the tail (enqueue to tail).voidvoidvoidvoidclear()Removes all values from this deque.booleanReturnstrueif this deque contains the specified element.booleancontainsAll(Object[] array) Exactly likecontainsAll(Collection), but takes an array instead of a Collection.booleancontainsAll(Object[] array, int offset, int length) booleancontainsAll(Collection<?> c) Returnstrueif this collection contains all the elements in the specified collection.booleancontainsAny(Object[] values) Returns true if this ObjectDeque contains any of the specified values.booleancontainsAny(Object[] values, int offset, int length) Returns true if this ObjectDeque contains any items from the specified range of values.booleancontainsAnyIterable(Iterable<?> values) Returns true if this ObjectDeque contains any of the specified values.Returns an iterator over the elements in this deque in reverse sequential order.descendingIterator(int index) Returns an iterator over the elements in this deque in reverse sequential order.booleanduplicateRange(int index, int count) Inserts the specified number of items at the specified index.element()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).voidensureCapacity(int additional) Increases the size of the backing array to accommodate the specified number of additional items.protected intensureGap(int index, int gapSize) Make sure there is a "gap" of exactlygapSizevalues starting atindex.booleanUsingObject.equals(Object)between each item in order, compares for equality with other types implementingListorQueue, including otherDequetypes.booleanfirst()Returns the first (head) item in the deque (without removing it).get(int index) Returns the element at the specified position in this deque.Gets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.getFirst()Retrieves, but does not remove, the first element of this deque.getLast()Retrieves, but does not remove, the last element of this deque.Gets a randomly selected item from this ObjectDeque.inthashCode()intReturns the index of the first occurrence of value in the deque, or -1 if no such value exists.intReturns the index of first occurrence of value in the deque, or -1 if no such value exists.intReturns the index of the first occurrence of value in the deque, or -1 if no such value exists.intReturns the index of first occurrence ofvaluein the deque, starting fromfromIndex, or -1 if no such value exists.booleanThis is an alias foradd(int, Object)that returnstrueto indicate it does modify this ObjectDeque.booleaninsertAll(int index, Collection<? extends T> c) An alias foraddAll(int, Collection); inserts all elements in the specified collection into this list at the specified position.booleanAlias foraddAll(int, Object[]).booleanAlias foraddAll(int, Object[], int, int).booleanisEmpty()Returns true if the deque is empty.iterator()Returns an iterator for the items in the deque.last()Returns the last (tail) item in the deque (without removing it).intlastIndexOf(Object value) Returns the index of the last occurrence of value in the deque, or -1 if no such value exists.intlastIndexOf(Object value, boolean identity) Returns the index of the last occurrence of value in the deque, or -1 if no such value exists.intlastIndexOf(Object value, int fromIndex) Returns the index of last occurrence ofvaluein the deque, starting fromfromIndexand going backwards, or -1 if no such value exists.intlastIndexOf(Object value, int fromIndex, boolean identity) Returns the index of last occurrence ofvaluein the deque, starting fromfromIndexand going backwards, or -1 if no such value exists.listIterator(int index) Gets an iterator over this deque that starts at the given index.booleannotEmpty()Returns true if the deque has one or more items.booleanInserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success andfalseif no space is currently available.booleanofferFirst(T t) Inserts the specified element at the front of this deque unless it would violate capacity restrictions.booleanInserts the specified element at the end of this deque unless it would violate capacity restrictions.static <T> ObjectDeque<T>parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.static <T> ObjectDeque<T>parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static <T> ObjectDeque<T>parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.peek()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returnsdefaultValueif this deque is empty.peekAt(int index) Returns the element at the specified position in this deque.Retrieves, but does not remove, the first element of this deque, or returnsdefaultValueif this deque is empty.peekLast()Retrieves, but does not remove, the last element of this deque, or returnsdefaultValueif this deque is empty.poll()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returnsdefaultValueif this deque is empty.poll(int index) Removes the element at the specified position in this deque.pollAt(int index) Removes the element at the specified position in this deque.Retrieves and removes the first element of this deque, or returnsdefaultValueif this deque is empty.pollLast()Retrieves and removes the last element of this deque, or returnsdefaultValueif this deque is empty.pop()Pops an element from the stack represented by this deque.voidPushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateExceptionif no space is currently available.Returns aTitem from anywhere in this ObjectDeque, chosen pseudo-randomly usingrandom.remove()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).remove(int index) Removes the element at the specified position in this deque.booleanRemoves the first occurrence of the specified element from this deque.booleanExactly likeremoveAll(Collection), but takes an array instead of a Collection.booleanbooleanremoveAll(Collection<?> other) Removes all of this collection's elements that are also contained in the specified collection (optional operation).removeAt(int index) Removes the element at the specified position in this deque.booleanremoveEach(Object[] array) Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.booleanremoveEach(Object[] array, int offset, int length) booleanremoveEachIterable(Iterable<?> other) Removes from this collection element-wise occurrences of elements contained in the specified other collection.Remove the first item from the deque.booleanRemoves the first occurrence of the specified element from this deque.Remove the last item from the deque.booleanRemoves the last occurrence of the specified element from this deque.booleanremoveLastValue(Object value, boolean identity) Removes the last instance of the specified value in the deque.voidremoveRange(int fromIndex, int toIndex) Removes from this list all the elements whose index is betweenfromIndex, inclusive, andtoIndex, exclusive.booleanremoveValue(Object value, boolean identity) Removes the first instance of the specified value in the deque.intreplaceAll(T find, T replace) Replaces every occurrence offindwithreplace.intreplaceAllIdentity(T find, T replace) Replaces every occurrence offindwithreplace.booleanreplaceFirst(T find, T replace) Replaces the first occurrence offindwithreplace.booleanreplaceFirstIdentity(T find, T replace) Replaces the first occurrence offindwithreplace.voidresize(int newSize) Resizes the backing array.booleanExactly likeAbstractCollection.retainAll(Collection), but takes an array instead of a Collection.booleanvoidreverse()Reverses this ObjectDeque in-place.reversed()Gets a new ObjectDeque (which is both List and Deque) that has had its order reversed.selectRanked(Comparator<T> comparator, int kthLowest) Selects the kth-lowest element from this ObjectDeque according to Comparator ranking.intselectRankedIndex(Comparator<T> comparator, int kthLowest) Gets the index of the kth-lowest element from this ObjectDeque according to Comparator ranking.Replaces the element at the specified position in this list with the specified element.voidsetDefaultValue(T defaultValue) Sets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque.voidshrink()Reduces the size of the backing array to the size of the actual items.voidPseudo-randomly shuffles the order of this Arrangeable in-place.intsize()Returns the number of elements in this deque.voidsort()Attempts to sort this deque in-place using its natural ordering, which requires T to implementComparableof T.voidsort(int from, int to) UsesObjectComparators.sort(Object[], int, int, Comparator)to sort a (clamped) subrange of this deque.voidsort(int from, int to, Comparator<? super T> comparator) Sorts this deque in-place usingObjectComparators.sort(Object[], int, int, Comparator).voidsort(Comparator<? super T> comparator) Sorts this deque in-place usingObjectComparators.sort(Object[], int, int, Comparator).voidsortJDK()Sorts this deque usingArrays.sort(Object[], int, int).voidsortJDK(int from, int to) UsesArrays.sort(Object[], int, int)to sort a (clamped) subrange of this deque.voidsortJDK(int from, int to, Comparator<? super T> comparator) UsesArrays.sort(Object[], int, int, Comparator)to sort a (clamped) subrange of this deque.voidsortJDK(Comparator<? super T> comparator) Sorts this deque usingArrays.sort(Object[], int, int, Comparator).subList(int fromIndex, int toIndex) voidswap(int first, int second) Switches the ordering of positionsfirstandsecond, without changing any items beyond that.Object[]toArray()Returns an array containing all the elements in this collection.<E> E[]toArray(E[] a) Returns an array containing all the elements in this collection; the runtime type of the returned array is that of the specified array.toString()Delegates toEnhancedCollection.toString(String, boolean)with a delimiter of", "and square brackets enabled.voidTrims the capacity of thisObjectDequeinstance to be the deque's current size.voidtruncate(int newSize) Reduces the size of the deque to the specified size by bulk-removing items from the tail end.voidtruncateFirst(int newSize) Reduces the size of the deque to the specified size by bulk-removing from the head.voidtruncateLast(int newSize) Alias fortruncate(int).static <T> ObjectDeque<T>with()Constructs an empty deque given the type as a generic type argument.static <T> ObjectDeque<T>with(T item) Creates a new ObjectDeque that holds only the given item, but can be resized.static <T> ObjectDeque<T>with(T... varargs) Creates a new ObjectDeque that will hold the items in the given array or varargs.static <T> ObjectDeque<T>with(T item0, T item1) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2, T item3) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2, T item3, T item4) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2, T item3, T item4, T item5) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new ObjectDeque that holds only the given items, but can be resized.static <T> ObjectDeque<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectDeque that holds only the given items, but can be resized.Methods inherited from class java.util.AbstractCollection
retainAllMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffleMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, removeAll, removeAllIterable, removeEach, toString, toString, toStringMethods inherited from interface com.github.tommyettinger.ds.Lisque
peekRandom, retainAllMethods inherited from interface java.util.List
replaceAll, spliterator
-
Field Details
-
defaultValue
The value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when callingpeek()on an empty deque. -
items
Contains the values in the deque. Head and tail indices go in a circle around this array, wrapping at the end. -
head
protected int headIndex of first element. Logically smaller than tail. Unless empty, it points to a valid element inside the deque. -
tail
protected int tailIndex of last element. Logically bigger than head. Unless empty, it points to a valid element inside the deque. This may be the same as head, and is if there is one element in the deque (or none), that will be the case. -
size
public int sizeNumber of elements in the deque. -
iterator1
-
iterator2
-
descendingIterator1
-
descendingIterator2
-
-
Constructor Details
-
ObjectDeque
public ObjectDeque()Creates a new ObjectDeque which can hold 16 values without needing to resize the backing array. -
ObjectDeque
public ObjectDeque(int initialSize) Creates a new ObjectDeque which can hold the specified number of values without needing to resize the backing array.- Parameters:
initialSize- how large the backing array should be, without any padding
-
ObjectDeque
Creates a new ObjectDeque using all the contents of the given Collection.- Parameters:
coll- a Collection of T that will be copied into this and used in full- Throws:
NullPointerException- ifcollisnull
-
ObjectDeque
Creates a new instance containing the items in the specified iterator.- Parameters:
iter- an iterator that will have its remaining contents added to this- Throws:
NullPointerException- ifiterisnull
-
ObjectDeque
Copies the given ObjectDeque exactly into this one. Individual values will be shallow-copied.- Parameters:
deque- another ObjectDeque to copy- Throws:
NullPointerException- ifdequeisnull
-
ObjectDeque
Creates a new ObjectDeque using all the contents of the given array.- Parameters:
a- an array of T that will be copied into this and used in full- Throws:
NullPointerException- ifaisnull
-
ObjectDeque
Creates a new ObjectDeque usingcountitems froma, starting atoffset. Ifcountis 0 or less, this will create an empty ObjectDeque with capacity 1.- Parameters:
a- an array of Toffset- where inato start using itemscount- how many items to use froma- Throws:
NullPointerException- ifaisnull
-
-
Method Details
-
getDefaultValue
Gets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque. Unless changed, the default value is usuallynull.- Returns:
- the current default value
-
setDefaultValue
Sets the default value, which is the value returned when nothing can be obtained from this deque and an exception is not meant to be thrown, such as when calling peek() on an empty deque. Unless changed, the default value is usuallynull.- Parameters:
defaultValue- any T object this can return instead of throwing an Exception, ornull
-
addLast
Appendsvalueto the tail (enqueue to tail). Unless backing array needs resizing, operates in O(1) time. -
addLast
-
addLast
-
addLast
-
addFirst
Prependsvalueto the head (enqueue to head). Unless backing array needs resizing, operates in O(1) time. -
addFirst
-
addFirst
-
addFirst
-
trimToSize
public void trimToSize()Trims the capacity of thisObjectDequeinstance to be the deque's current size. An application can use this operation to minimize the storage of anObjectDequeinstance. -
ensureCapacity
public void ensureCapacity(int additional) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes. -
shrink
public void shrink()Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added. This is an alias fortrimToSize(). -
resize
public void resize(int newSize) Resizes the backing array. newSize should be greater than the current size; otherwise, newSize will be set to size and the resize to the same size will (for most purposes) be wasted effort. If this is not empty, this will rearrange the items internally to be linear and have the head at index 0, with the tail atsize - 1. This always allocates a new internal backing array. -
ensureGap
protected int ensureGap(int index, int gapSize) Make sure there is a "gap" of exactlygapSizevalues starting atindex. This can resize the backing array to achieve this goal. If possible, this will keep the same backing array and modify it in-place. The "gap" is not assigned null, and may contain old/duplicate references; calling code must overwrite the entire gap with additional values to ensure GC correctness.- Parameters:
index- the 0-based index in the iteration order where the gap will be presentgapSize- the number of items that will need filling in the gap, and can be filled without issues.- Returns:
- the position in the array where the gap will begin, which is unrelated to the index
- "Implementation Note"
- This is considered an incomplete modification for the purpose of
AbstractList.modCount, so it does not change modCount; the code that fills in the gap should change modCount instead.
-
duplicateRange
public boolean duplicateRange(int index, int count) Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion, and the previous values will be pushed to after the duplicated range.- Parameters:
index- the first index to duplicatecount- how many items to duplicate
-
removeFirst
Remove the first item from the deque. (dequeue from head) Always O(1).- Specified by:
removeFirstin interfaceDeque<T>- Specified by:
removeFirstin interfaceLisque<T>- Returns:
- removed object
- Throws:
NoSuchElementException- when the deque is empty
-
removeLast
Remove the last item from the deque. (dequeue from tail) Always O(1).- Specified by:
removeLastin interfaceDeque<T>- Specified by:
removeLastin interfaceLisque<T>- Returns:
- removed object
- Throws:
NoSuchElementException- when the deque is empty- See Also:
-
reversed
Gets a new ObjectDeque (which is both List and Deque) that has had its order reversed. Doesn't create a view; this does actually copy the ObjectDeque. Consider usingdescendingIterator()to get a reverse-order Iterator over this collection. This runs in O(n) time, and iterates over this deque once (in descending order) to add all items to a new ObjectDeque usingEnhancedCollection.addAll(Iterator). -
offerFirst
Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theaddFirst(T)method, which can fail to insert an element only by throwing an exception.- Specified by:
offerFirstin interfaceDeque<T>- Specified by:
offerFirstin interfaceLisque<T>- Parameters:
t- the element to add- Returns:
trueif the element was added to this deque, elsefalse- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this dequeNullPointerException- if the specified element is null and this deque does not permit null elementsIllegalArgumentException- if some property of the specified element prevents it from being added to this deque
-
offerLast
Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theaddLast(T)method, which can fail to insert an element only by throwing an exception.- Specified by:
offerLastin interfaceDeque<T>- Specified by:
offerLastin interfaceLisque<T>- Parameters:
t- the element to add- Returns:
trueif the element was added to this deque, elsefalse- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this dequeNullPointerException- if the specified element is null and this deque does not permit null elementsIllegalArgumentException- if some property of the specified element prevents it from being added to this deque
-
pollFirst
Retrieves and removes the first element of this deque, or returnsdefaultValueif this deque is empty. The default value is usuallynullunless it has been changed withsetDefaultValue(Object). -
pollLast
Retrieves and removes the last element of this deque, or returnsdefaultValueif this deque is empty. The default value is usuallynullunless it has been changed withsetDefaultValue(Object). -
getFirst
Retrieves, but does not remove, the first element of this deque.This method differs from
peekFirstonly in that it throws an exception if this deque is empty. -
getLast
Retrieves, but does not remove, the last element of this deque. This method differs frompeekLastonly in that it throws an exception if this deque is empty. -
peekFirst
Retrieves, but does not remove, the first element of this deque, or returnsdefaultValueif this deque is empty. -
peekLast
Retrieves, but does not remove, the last element of this deque, or returnsdefaultValueif this deque is empty. -
removeFirstOccurrence
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first elementesuch thatObjects.equals(o, e)(if such an element exists). Returnstrueif this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
removeFirstOccurrencein interfaceDeque<T>- Specified by:
removeFirstOccurrencein interfaceLisque<T>- Parameters:
o- element to be removed from this deque, if present- Returns:
trueif an element was removed as a result of this call- Throws:
ClassCastException- if the class of the specified element is incompatible with this deque (optional)NullPointerException- if the specified element is null and this deque does not permit null elements (optional)
-
removeLastOccurrence
Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last elementesuch thatObjects.equals(o, e)(if such an element exists). Returnstrueif this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
removeLastOccurrencein interfaceDeque<T>- Specified by:
removeLastOccurrencein interfaceLisque<T>- Parameters:
o- element to be removed from this deque, if present- Returns:
trueif an element was removed as a result of this call- Throws:
ClassCastException- if the class of the specified element is incompatible with this deque (optional)NullPointerException- if the specified element is null and this deque does not permit null elements (optional)
-
add
Inserts the specified element into the deque represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success and throwing anIllegalStateExceptionif no space is currently available. When using a capacity-restricted deque, it is generally preferable to useoffer.This method is equivalent to
addLast(T).- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceDeque<T>- Specified by:
addin interfaceLisque<T>- Specified by:
addin interfaceList<T>- Specified by:
addin interfaceQueue<T>- Overrides:
addin classAbstractList<T>- Parameters:
t- the element to add- Returns:
true(as specified byCollection.add(E))- Throws:
IllegalStateException- if the element cannot be added at this time due to capacity restrictionsClassCastException- if the class of the specified element prevents it from being added to this dequeNullPointerException- if the specified element is null and this deque does not permit null elementsIllegalArgumentException- if some property of the specified element prevents it from being added to this deque
-
add
Description copied from interface:EnhancedCollectionAdds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Specified by:
addin interfaceEnhancedCollection<T>- Parameters:
item0- a T itemitem1- a T item- Returns:
- true if this modified the set
-
add
Description copied from interface:EnhancedCollectionAdds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Specified by:
addin interfaceEnhancedCollection<T>- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- true if this modified the set
-
add
Description copied from interface:EnhancedCollectionAdds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Specified by:
addin interfaceEnhancedCollection<T>- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- true if this modified the set
-
add
Inserts the specified element into this deque at the specified index. UnlikeofferFirst(Object)andofferLast(Object), this does not run in expected constant time unless the index is less than or equal to 0 (where it acts like offerFirst()) or greater than or equal tosize()(where it acts like offerLast()). -
insert
This is an alias foradd(int, Object)that returnstrueto indicate it does modify this ObjectDeque. -
offer
Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success andfalseif no space is currently available. When using a capacity-restricted deque, this method is generally preferable to theadd(T)method, which can fail to insert an element only by throwing an exception.This method is equivalent to
offerLast(T).- Specified by:
offerin interfaceDeque<T>- Specified by:
offerin interfaceLisque<T>- Specified by:
offerin interfaceQueue<T>- Parameters:
t- the element to add- Returns:
trueif the element was added to this deque, elsefalse- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this dequeNullPointerException- if the specified element is null and this deque does not permit null elementsIllegalArgumentException- if some property of the specified element prevents it from being added to this deque
-
remove
Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs frompoll()only in that it throws an exception if this deque is empty.This method is equivalent to
removeFirst(). -
poll
Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returnsdefaultValueif this deque is empty.This method is equivalent to
pollFirst(). -
element
Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). This method differs frompeekonly in that it throws an exception if this deque is empty.This method is equivalent to
getFirst(). -
peek
Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returnsdefaultValueif this deque is empty.This method is equivalent to
peekFirst(). -
addAll
Adds all the elements in the specified collection at the end of this deque, as if by callingaddLast(T)on each one, in the order that they are returned by the collection's iterator.When using a capacity-restricted deque, it is generally preferable to call
offerseparately on each element.An exception encountered while trying to add an element may result in only some of the elements having been successfully added when the associated exception is thrown.
- Specified by:
addAllin interfaceCollection<T>- Specified by:
addAllin interfaceDeque<T>- Specified by:
addAllin interfaceList<T>- Overrides:
addAllin classAbstractCollection<T>- Parameters:
c- the elements to be inserted into this deque- Returns:
trueif this deque changed as a result of the call- Throws:
IllegalStateException- if not all the elements can be added at this time due to insertion restrictionsClassCastException- if the class of an element of the specified collection prevents it from being added to this dequeNullPointerException- if the specified collection contains a null element and this deque does not permit null elements, or if the specified collection is nullIllegalArgumentException- if some property of an element of the specified collection prevents it from being added to this deque
-
addAllLast
An alias foraddAll(Collection), this adds every item incto this in order at the end.- Specified by:
addAllLastin interfaceLisque<T>- Parameters:
c- the elements to be inserted into this deque- Returns:
trueif this deque changed as a result of the call
-
addAllFirst
Adds every item incto this in order at the start. The iteration order ofcwill be preserved for the added items.- Specified by:
addAllFirstin interfaceLisque<T>- Parameters:
c- the elements to be inserted into this deque- Returns:
trueif this deque changed as a result of the call
-
insertAll
An alias foraddAll(int, Collection); inserts all elements in the specified collection into this list at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in 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. (Note that this will occur if the specified collection is this list, and it's nonempty.) -
addAll
-
addAll
Exactly likeaddAll(Collection), but takes an array instead of a Collection. -
addAll
- Specified by:
addAllin interfaceEnhancedCollection<T>- Specified by:
addAllin interfaceLisque<T>- Parameters:
array- the elements to be inserted into this dequeoffset- the index of the first item in array to addlength- how many items, at most, to add from array into this- Returns:
trueif this deque changed as a result of the call- See Also:
-
addAllLast
An alias foraddAll(Object[]).- Specified by:
addAllLastin interfaceLisque<T>- Parameters:
array- the elements to be inserted into this deque- Returns:
trueif this deque changed as a result of the call- See Also:
-
addAllLast
An alias foraddAll(Object[], int, int).- Specified by:
addAllLastin interfaceLisque<T>- Parameters:
array- the elements to be inserted into this dequeoffset- the index of the first item in array to addlength- how many items, at most, to add from array into this- Returns:
trueif this deque changed as a result of the call- See Also:
-
addAllFirst
Exactly likeaddAllFirst(Collection), but takes an array instead of a Collection.- Specified by:
addAllFirstin interfaceLisque<T>- Parameters:
array- the elements to be inserted into this deque- Returns:
trueif this deque changed as a result of the call- See Also:
-
addAllFirst
LikeaddAllFirst(Object[]), but only uses at mostlengthitems fromarray, starting atoffset. The order ofarraywill be preserved, starting at the head of the deque.- Specified by:
addAllFirstin interfaceLisque<T>- Parameters:
array- the elements to be inserted into this dequeoffset- the index of the first item in array to addlength- how many items, at most, to add from array into this- Returns:
trueif this deque changed as a result of the call- See Also:
-
insertAll
Alias foraddAll(int, Object[]). -
insertAll
Alias foraddAll(int, Object[], int, int).- Specified by:
insertAllin interfaceLisque<T>- Parameters:
index- the index in this deque's iteration order to place the first item inarrayarray- the elements to be inserted into this dequeoffset- the index of the first item in array to addlength- how many items, at most, to add from array into this- Returns:
trueif this deque changed as a result of the call
-
addAll
LikeaddAll(int, Collection), but takes an array instead of a Collection and inserts it so the first item will be at the givenindex. The order ofarraywill be preserved, starting at the given index in this deque. -
addAll
LikeaddAll(int, Collection), but takes an array instead of a Collection, gets items starting atoffsetfrom that array, usinglengthitems, and inserts them so the item at the given offset will be at the givenindex. The order ofarraywill be preserved, starting at the given index in this deque.- Specified by:
addAllin interfaceLisque<T>- Parameters:
index- the index in this deque's iteration order to place the first item inarrayarray- the elements to be inserted into this dequeoffset- the index of the first item in array to addlength- how many items, at most, to add from array into this- Returns:
trueif this deque changed as a result of the call- See Also:
-
push
Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateExceptionif no space is currently available.This method is equivalent to
addFirst(T).- Specified by:
pushin interfaceDeque<T>- Specified by:
pushin interfaceLisque<T>- Parameters:
t- the element to push- Throws:
IllegalStateException- if the element cannot be added at this time due to capacity restrictionsClassCastException- if the class of the specified element prevents it from being added to this dequeNullPointerException- if the specified element is null and this deque does not permit null elementsIllegalArgumentException- if some property of the specified element prevents it from being added to this deque
-
pop
Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.This method is equivalent to
removeFirst(). -
remove
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first elementesuch thatObjects.equals(o, e)(if such an element exists). Returnstrueif this deque contained the specified element (or equivalently, if this deque changed as a result of the call).This method is equivalent to
removeFirstOccurrence(Object).- Specified by:
removein interfaceCollection<T>- Specified by:
removein interfaceDeque<T>- Specified by:
removein interfaceList<T>- Overrides:
removein classAbstractCollection<T>- Parameters:
o- element to be removed from this deque, if present- Returns:
trueif an element was removed as a result of this call- Throws:
ClassCastException- if the class of the specified element is incompatible with this deque (optional)NullPointerException- if the specified element is null and this deque does not permit null elements (optional)
-
contains
Returnstrueif this deque contains the specified element. More formally, returnstrueif and only if this deque contains at least one elementesuch thatObjects.equals(o, e).- Specified by:
containsin interfaceCollection<T>- Specified by:
containsin interfaceDeque<T>- Specified by:
containsin interfaceLisque<T>- Specified by:
containsin interfaceList<T>- Overrides:
containsin classAbstractCollection<T>- Parameters:
o- element whose presence in this deque is to be tested- Returns:
trueif this deque contains the specified element- Throws:
ClassCastException- if the class of the specified element is incompatible with this deque (optional)NullPointerException- if the specified element is null and this deque does not permit null elements (optional)
-
size
public int size()Returns the number of elements in this deque.- Specified by:
sizein interfaceArrangeable- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceDeque<T>- Specified by:
sizein interfaceList<T>- Specified by:
sizein classAbstractCollection<T>- Returns:
- the number of elements in this deque
-
toArray
Returns an array containing all the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type isObject.The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
- Specified by:
toArrayin interfaceCollection<T>- Specified by:
toArrayin interfaceList<T>- Overrides:
toArrayin classAbstractCollection<T>- Returns:
- an array, whose runtime component
type is
Object, containing all the elements in this collection
-
toArray
public <E> E[] toArray(E[] a) Returns an array containing all the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to
null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain anynullelements.)If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
- Specified by:
toArrayin interfaceCollection<T>- Specified by:
toArrayin interfaceList<T>- Overrides:
toArrayin classAbstractCollection<T>- Parameters:
a- the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing all the elements in this collection
- Throws:
ArrayStoreException- if the runtime type of any element in this collection is not assignable to the runtime component type of the specified arrayNullPointerException- if the specified array is null
-
containsAll
Returnstrueif this collection contains all the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceLisque<T>- Specified by:
containsAllin interfaceList<T>- Overrides:
containsAllin classAbstractCollection<T>- Parameters:
c- collection to be checked for containment in this collection- Returns:
trueif this collection contains all the elements in the specified collection- Throws:
ClassCastException- if the types of one or more elements in the specified collection are incompatible with this collection (optional)NullPointerException- if the specified collection contains one or more null elements and this collection does not permit null elements (optional), or if the specified collection is null.- See Also:
-
containsAll
Exactly likecontainsAll(Collection), but takes an array instead of a Collection.- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
array- array to be checked for containment in this deque- Returns:
trueif this deque contains all the elements in the specified array- See Also:
-
containsAll
- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
array- array to be checked for containment in this dequeoffset- the index of the first item in array to checklength- how many items, at most, to check from array- Returns:
trueif this deque contains all the elements in the specified range of array- See Also:
-
containsAnyIterable
Returns true if this ObjectDeque contains any of the specified values.- Specified by:
containsAnyIterablein interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itself- Returns:
- true if this ObjectDeque contains any of the items in
values, false otherwise
-
containsAny
Returns true if this ObjectDeque contains any of the specified values.- Specified by:
containsAnyin interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itself- Returns:
- true if this ObjectDeque contains any of the items in
values, false otherwise
-
containsAny
Returns true if this ObjectDeque contains any items from the specified range of values.- Specified by:
containsAnyin interfaceEnhancedCollection<T>- Parameters:
values- may contain nulls, but must not be null itselfoffset- the index to start checking in valueslength- how many items to check from values- Returns:
- true if this ObjectDeque contains any of the items in the given range of
values, false otherwise
-
removeAll
Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.- Specified by:
removeAllin interfaceCollection<T>- Specified by:
removeAllin interfaceLisque<T>- Specified by:
removeAllin interfaceList<T>- Overrides:
removeAllin classAbstractCollection<T>- Parameters:
other- collection containing elements to be removed from this collection- Returns:
trueif this deque changed as a result of the call- Throws:
UnsupportedOperationException- if theremoveAllmethod is not supported by this collectionClassCastException- if the types of one or more elements in this collection are incompatible with the specified collection (optional)NullPointerException- if this collection contains one or more null elements and the specified collection does not support null elements (optional), or if the specified collection is null- See Also:
-
removeAll
Exactly likeremoveAll(Collection), but takes an array instead of a Collection.- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
other- array containing elements to be removed from this collection- Returns:
trueif this deque changed as a result of the call- See Also:
-
removeAll
- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
array- the elements to be removed from this dequeoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this deque changed as a result of the call- See Also:
-
removeEachIterable
Removes from this collection element-wise occurrences of elements contained in the specified other collection. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this collection or has additional items, then removing each ofotherwill clear this.- Specified by:
removeEachIterablein interfaceEnhancedCollection<T>- Parameters:
other- a Collection of items to remove one-by-one, such as an ObjectList or an ObjectSet- Returns:
- true if this deque was modified.
-
removeEach
Exactly likeremoveEachIterable(Iterable), but takes an array instead of a Collection.- Specified by:
removeEachin interfaceEnhancedCollection<T>- Parameters:
array- array containing elements to be removed from this collection- Returns:
trueif this deque changed as a result of the call- See Also:
-
removeEach
- Specified by:
removeEachin interfaceEnhancedCollection<T>- Parameters:
array- the elements to be removed from this dequeoffset- the index of the first item in array to removelength- how many items, at most, to get from array and remove from this- Returns:
trueif this deque changed as a result of the call- See Also:
-
retainAll
Exactly likeAbstractCollection.retainAll(Collection), but takes an array instead of a Collection. -
retainAll
-
selectRanked
Selects the kth-lowest element from this ObjectDeque according to Comparator ranking. This might partially sort the ObjectDeque, changing its order. The ObjectDeque must have a size greater than 0, or aRuntimeExceptionwill be thrown.- Parameters:
comparator- used for comparisonkthLowest- rank of desired object according to comparison; k is based on ordinal numbers, not array indices. For min value use 1, for max value use size of the ObjectDeque; using 0 results in a runtime exception.- Returns:
- the value of the kth lowest ranked object.
- See Also:
-
selectRankedIndex
Gets the index of the kth-lowest element from this ObjectDeque according to Comparator ranking. This might partially sort the ObjectDeque, changing its order. The ObjectDeque must have a size greater than 0, or aRuntimeExceptionwill be thrown.- Parameters:
comparator- used for comparisonkthLowest- rank of desired object according to comparison; k is based on ordinal numbers, not array indices. For min value use 1, for max value use size of the ObjectDeque; using 0 results in a runtime exception.- Returns:
- the index of the kth lowest ranked object.
- See Also:
-
truncateLast
public void truncateLast(int newSize) Alias fortruncate(int).- Specified by:
truncateLastin interfaceLisque<T>- Parameters:
newSize- the size this deque should have after this call completes, if smaller than the current size
-
truncate
public void truncate(int newSize) Reduces the size of the deque to the specified size by bulk-removing items from the tail end. If the deque is already smaller than the specified size, no action is taken. -
truncateFirst
public void truncateFirst(int newSize) Reduces the size of the deque to the specified size by bulk-removing from the head. If the deque is already smaller than the specified size, no action is taken.- Specified by:
truncateFirstin interfaceLisque<T>- Parameters:
newSize- the size this deque should have after this call completes, if smaller than the current size
-
removeRange
public void removeRange(int fromIndex, int toIndex) Removes from this list all the elements whose index is betweenfromIndex, inclusive, andtoIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by(toIndex - fromIndex)elements. IftoIndex==fromIndex, this operation has no effect. IffromIndexis 0 or less, this delegates totruncateFirst(int); iftoIndexis equal to or greater than the size of this collection, this delegates totruncate(int).
This is public here, not protected as in most JDK collections, because there are actually sometimes needs for this in user code.- Specified by:
removeRangein interfaceLisque<T>- Overrides:
removeRangein classAbstractList<T>- Parameters:
fromIndex- index of first element to be removed (inclusive)toIndex- index after last element to be removed (exclusive)
-
indexOf
Returns the index of the first occurrence of value in the deque, or -1 if no such value exists. Uses .equals() to compare items. -
indexOf
Returns the index of the first occurrence of value in the deque, or -1 if no such value exists. Uses .equals() to compare items. This returnsfromIndexifvalueis present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be larger than the last-returned index.- Specified by:
indexOfin interfaceLisque<T>- Parameters:
value- the Object to look for, which may be nullfromIndex- the initial index to check (zero-indexed, starts at the head, inclusive)- Returns:
- An index of first occurrence of value at or after fromIndex in the deque, or -1 if no such value exists
-
indexOf
Returns the index of first occurrence of value in the deque, or -1 if no such value exists. Whenidentityis false, uses .equals() to compare items; when identity is true, uses==.- Parameters:
value- the Object to look for, which may be nullidentity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of first occurrence of value in the deque or -1 if no such value exists
-
indexOf
Returns the index of first occurrence ofvaluein the deque, starting fromfromIndex, or -1 if no such value exists. This returnsfromIndexifvalueis present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be larger than the last-returned index. Whenidentityis false, uses .equals() to compare items; when identity is true, uses==.- Parameters:
value- the Object to look for, which may be nullfromIndex- the initial index to check (zero-indexed, starts at the head, inclusive)identity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of first occurrence of value at or after fromIndex in the deque, or -1 if no such value exists
-
lastIndexOf
Returns the index of the last occurrence of value in the deque, or -1 if no such value exists. Uses .equals() to compare items.- Specified by:
lastIndexOfin interfaceLisque<T>- Specified by:
lastIndexOfin interfaceList<T>- Overrides:
lastIndexOfin classAbstractList<T>- Parameters:
value- the Object to look for, which may be null- Returns:
- An index of the last occurrence of value in the deque or -1 if no such value exists
-
lastIndexOf
Returns the index of last occurrence ofvaluein the deque, starting fromfromIndexand going backwards, or -1 if no such value exists. This returnsfromIndexifvalueis present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be smaller than the last-returned index. Uses .equals() to compare items.- Specified by:
lastIndexOfin interfaceLisque<T>- Parameters:
value- the Object to look for, which may be nullfromIndex- the initial index to check (zero-indexed, starts at the head, inclusive)- Returns:
- An index of last occurrence of value at or before fromIndex in the deque, or -1 if no such value exists
-
lastIndexOf
Returns the index of the last occurrence of value in the deque, or -1 if no such value exists.- Parameters:
value- the Object to look for, which may be nullidentity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of the last occurrence of value in the deque or -1 if no such value exists
-
lastIndexOf
Returns the index of last occurrence ofvaluein the deque, starting fromfromIndexand going backwards, or -1 if no such value exists. This returnsfromIndexifvalueis present at that point, so if you chain calls to indexOf(), the subsequent fromIndex should be smaller than the last-returned index. Whenidentityis false, uses .equals() to compare items; when identity is true, uses==.- Parameters:
value- the Object to look for, which may be nullfromIndex- the initial index to check (zero-indexed, starts at the head, inclusive)identity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of last occurrence of value at or before fromIndex in the deque, or -1 if no such value exists
-
listIterator
- Specified by:
listIteratorin interfaceLisque<T>- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classAbstractList<T>
-
listIterator
Gets an iterator over this deque that starts at the given index.- Specified by:
listIteratorin interfaceLisque<T>- Specified by:
listIteratorin interfaceList<T>- Overrides:
listIteratorin classAbstractList<T>- Parameters:
index- the index to start iterating from in this deque- Returns:
- a reused iterator starting at the given index
-
subList
-
removeValue
Removes the first instance of the specified value in the deque.- Parameters:
identity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if value was found and removed, false otherwise
-
removeLastValue
Removes the last instance of the specified value in the deque.- Parameters:
identity- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if value was found and removed, false otherwise
-
removeAt
Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque.
This is an alias forremove(int)for compatibility with primitive-backed lists and deques;remove(int)can refer to the method that removes an item by value, not by index, in those types. -
remove
Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque. -
pollAt
Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque, orthe default valueif this is empty. This will not throw an Exception in normal usage, even if index is negative (which makes this simply returnpollFirst()) or greater than or equal tosize()(which makes this returnpollLast()).
This is an alias forpoll(int)for compatibility with primitive-backed lists and deques;poll(int)can refer to the method that removes an item by value, not by index, in those types. -
poll
Removes the element at the specified position in this deque. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the deque, orthe default valueif this is empty. This will not throw an Exception in normal usage, even if index is negative (which makes this simply returnpollFirst()) or greater than or equal tosize()(which makes this returnpollLast()). -
notEmpty
public boolean notEmpty()Returns true if the deque has one or more items. -
isEmpty
public boolean isEmpty()Returns true if the deque is empty. -
first
Returns the first (head) item in the deque (without removing it).- Specified by:
firstin interfaceEnhancedCollection<T>- Returns:
- the first item in this EnhancedCollection, as produced by
Collection.iterator() - Throws:
NoSuchElementException- when the deque is empty- See Also:
-
last
Returns the last (tail) item in the deque (without removing it).- Specified by:
lastin interfaceLisque<T>- Throws:
NoSuchElementException- when the deque is empty- See Also:
-
get
Returns the element at the specified position in this deque. LikeArrayListorObjectList, but unlikeLinkedList, this runs in O(1) time. It is expected to be slightly slower thanArrayList.get(int), which also runs in O(1) time. Unlike get() in ArrayList or ObjectList, this considers negative indices to refer to the first item, and too-large indices to refer to the last item. That means it delegates togetFirst()orgetLast()in those cases instead of throwing anIndexOutOfBoundsException, though it may throw aNoSuchElementExceptionif the deque is empty and there is no item it can get.- Specified by:
getin interfaceList<T>- Specified by:
getin classAbstractList<T>- Parameters:
index- index of the element to return- Returns:
- the element at the specified position in this deque
- Throws:
NoSuchElementException- if the deque is empty
-
peekAt
Returns the element at the specified position in this deque. LikeArrayListorObjectList, but unlikeLinkedList, this runs in O(1) time. It is expected to be slightly slower thanArrayList.get(int), which also runs in O(1) time. Unlike get() in ArrayList or ObjectList, this considers negative indices to refer to the first item, and too-large indices to refer to the last item. That means it delegates topeekFirst()orpeekLast()in those cases instead of throwing anIndexOutOfBoundsException, and it will returnthe default valueif the deque is empty. Unless changed, the default value is usuallynull. -
set
Replaces the element at the specified position in this list with the specified element. If this deque is empty or the index is larger than the largest index currently in this deque, this delegates toaddLast(Object)and returnsthe default value. If the index is negative, this delegates toaddFirst(Object)and returnsthe default value.- Specified by:
setin interfaceLisque<T>- Specified by:
setin interfaceList<T>- Overrides:
setin classAbstractList<T>- Parameters:
index- index of the element to replaceitem- element to be stored at the specified position- Returns:
- the element previously at the specified position
- Throws:
ClassCastException- if the class of the specified element prevents it from being put in this list
-
clear
public void clear()Removes all values from this deque. Values in backing array are set to null to prevent memory leaks, so this operates in O(n) time.- Specified by:
clearin interfaceCollection<T>- Specified by:
clearin interfaceList<T>- Overrides:
clearin classAbstractList<T>
-
replaceFirst
Replaces the first occurrence offindwithreplace. Returns true if it performed the replacement, or false if there was nothing to replace. This also returns false if find and replace are the same. This compares T items byObject.equals(Object), unless an item is null.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- true if this changed, or false otherwise
-
replaceAll
Replaces every occurrence offindwithreplace. Returns the number of changed items, which is 0 if nothing was found or in the case that find and replace are the same. This compares T items byObject.equals(Object), unless an item is null.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- the number of replacements that occurred; 0 if nothing was found or replaced
-
replaceFirstIdentity
Replaces the first occurrence offindwithreplace. Returns true if it performed the replacement, or false if there was nothing to replace. This also returns false if find and replace are the same. This compares T items by identity, not withObject.equals(Object)! This may be useful to replace an occurrence ofnullby a givenreplacevalue.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- true if this changed, or false otherwise
-
replaceAllIdentity
Replaces every occurrence offindwithreplace. Returns the number of changed items, which is 0 if nothing was found or in the case that find and replace are the same. This compares T items by identity, not withObject.equals(Object)! This may be useful to replace occurrences ofnullby a givenreplacevalue.- Parameters:
find- the item to search forreplace- the item to replacefindwith, if possible- Returns:
- the number of replacements that occurred; 0 if nothing was found or replaced
-
iterator
Returns an iterator for the items in the deque. Remove is supported.
Reuses one of two iterators for this deque. For nested or multithreaded iteration, useObjectDequeIterator(ObjectDeque). -
descendingIterator
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).
Reuses one of two descending iterators for this deque. For nested or multithreaded iteration, useObjectDequeIterator(ObjectDeque, boolean).- Specified by:
descendingIteratorin interfaceDeque<T>- Specified by:
descendingIteratorin interfaceLisque<T>- Returns:
- an iterator over the elements in this deque in reverse sequence
-
descendingIterator
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order fromindexbackwards to first (head).
Reuses one of two descending iterators for this deque. For nested or multithreaded iteration, useObjectDequeIterator(ObjectDeque, boolean).- Parameters:
index- the index to start iterating from in this deque- Returns:
- an iterator over the elements in this deque in reverse sequence
-
toString
Delegates toEnhancedCollection.toString(String, boolean)with a delimiter of", "and square brackets enabled.- Overrides:
toStringin classAbstractCollection<T>- Returns:
- the square-bracketed String representation of this ObjectDeque, with items separated by ", "
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceList<T>- Overrides:
hashCodein classAbstractList<T>
-
equals
UsingObject.equals(Object)between each item in order, compares for equality with other types implementingListorQueue, including otherDequetypes. Ifois not a List or Queue (and is also not somehow reference-equivalent to this collection), this returns false. This uses theIterable.iterator()of both this ando, so if either is in the middle of a concurrent iteration that modifies the Collection, this may fail.- Specified by:
equalsin interfaceCollection<T>- Specified by:
equalsin interfaceList<T>- Overrides:
equalsin classAbstractList<T>- Parameters:
o- object to be compared for equality with this collection- Returns:
- true if this is equal to o, or false otherwise
-
equalsIdentity
Using==between each item in order, compares for equality with other types implementingListorQueue, including otherDequetypes. Ifois not a List or Queue (and is also not somehow reference-equivalent to this collection), this returns false. This uses theIterable.iterator()of both this ando, so if either is in the middle of a concurrent iteration that modifies the Collection, this may fail.- Parameters:
o- object to be compared for equality with this collection- Returns:
- true if this is equal to o, or false otherwise
-
swap
public void swap(int first, int second) Switches the ordering of positionsfirstandsecond, without changing any items beyond that.- Specified by:
swapin interfaceArrangeable- Parameters:
first- the first position, must not be negative and must be less thansize()second- the second position, must not be negative and must be less thansize()
-
reverse
public void reverse()Reverses this ObjectDeque in-place.- Specified by:
reversein interfaceArrangeable
-
shuffle
Description copied from interface:ArrangeablePseudo-randomly shuffles the order of this Arrangeable in-place.- Specified by:
shufflein interfaceArrangeable- Parameters:
rng- anyRandom, such asAlternateRandomor one from juniper
-
sort
public void sort()Attempts to sort this deque in-place using its natural ordering, which requires T to implementComparableof T. UsesObjectComparators.sort(Object[], int, int, Comparator). -
sort
public void sort(int from, int to) UsesObjectComparators.sort(Object[], int, int, Comparator)to sort a (clamped) subrange of this deque. Sorts using the natural ordering of T, which requires T to implementComparableof T. This only sorts between indicesfrom(inclusive) andto(exclusive).- Parameters:
from- first index to use, inclusiveto- last index to use, exclusive
-
sort
Sorts this deque in-place usingObjectComparators.sort(Object[], int, int, Comparator). This should operate in O(n log(n)) time or less when the internals of the deque are continuous (the head is before the tail in the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value forcomparatorif T implementsComparableof T, which will make this use the natural ordering for T. -
sort
Sorts this deque in-place usingObjectComparators.sort(Object[], int, int, Comparator). This only sorts between indicesfrom(inclusive) andto(exclusive). This should operate in O(n log(n)) time or less when the subrange of the deque is continuous (fromis beforetoin the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value forcomparatorif T implementsComparableof T, which will make this use the natural ordering for T.- Parameters:
comparator- the Comparator to use for T items; may be null to use the natural order of T items when T implements Comparable of T
-
sortJDK
public void sortJDK()Sorts this deque usingArrays.sort(Object[], int, int). This should operate in O(n log(n)) time or less when the internals of the deque are continuous (the head is before the tail in the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. This requires T to implementComparableof T, and this will use the natural ordering for T. -
sortJDK
Sorts this deque usingArrays.sort(Object[], int, int, Comparator). This should operate in O(n log(n)) time or less when the internals of the deque are continuous (the head is before the tail in the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value forcomparatorif T implementsComparableof T, which will make this use the natural ordering for T.- Parameters:
comparator- the Comparator to use for T items; may be null to use the natural order of T items when T implements Comparable of T
-
sortJDK
public void sortJDK(int from, int to) UsesArrays.sort(Object[], int, int)to sort a (clamped) subrange of this deque. This only sorts between indicesfrom(inclusive) andto(exclusive). This should operate in O(n log(n)) time or less when the subrange of the deque is continuous (fromis beforetoin the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value forcomparatorif T implementsComparableof T, which will make this use the natural ordering for T.- Parameters:
from- first index to use, inclusiveto- last index to use, exclusive
-
sortJDK
UsesArrays.sort(Object[], int, int, Comparator)to sort a (clamped) subrange of this deque. This only sorts between indicesfrom(inclusive) andto(exclusive). This should operate in O(n log(n)) time or less when the subrange of the deque is continuous (fromis beforetoin the array). If the internals are not continuous, this takes an additional O(n) step (where n is less than the size of the deque) to rearrange the internals before sorting. You can pass null as the value forcomparatorif T implementsComparableof T, which will make this use the natural ordering for T.- Parameters:
from- first index to use, inclusiveto- last index to use, exclusivecomparator- the Comparator to use for T items; may be null to use the natural order of T items when T implements Comparable of T
-
getRandom
Gets a randomly selected item from this ObjectDeque. Throws aNoSuchElementExceptionif empty.- Parameters:
random- any Random or subclass of it, such asAlternateRandom.- Returns:
- a randomly selected item from this deque
-
random
Returns aTitem from anywhere in this ObjectDeque, chosen pseudo-randomly usingrandom. If this ObjectDeque is empty, returnsgetDefaultValue(). -
with
Constructs an empty deque given the type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Type Parameters:
T- the type of items; must be given explicitly- Returns:
- a new deque containing nothing
-
with
Creates a new ObjectDeque that holds only the given item, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item- one T item- Returns:
- a new ObjectDeque that holds the given item
-
with
Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
public static <T> ObjectDeque<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
public static <T> ObjectDeque<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new ObjectDeque that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T itemitem7- a T item- Returns:
- a new ObjectDeque that holds the given items
-
with
Creates a new ObjectDeque that will hold the items in the given array or varargs. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.- Type Parameters:
T- the type of items, typically inferred by all the items being the same type- Parameters:
varargs- either 0 or more T items, or an array of T- Returns:
- a new ObjectDeque that holds the given T items
-
parse
Callsparse(String, String, PartialParser, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstr- Returns:
- a new collection parsed from str
-
parse
public static <T> ObjectDeque<T> parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstrbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
public static <T> ObjectDeque<T> parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-