Class TrackedDeque<E extends TrackedDeque.Element>
java.lang.Object
de.tomatengames.util.data.TrackedDeque<E>
- Type Parameters:
E- The type of the elements in the deque. Must extendTrackedDeque.Element.
- All Implemented Interfaces:
Iterable<E>
A deque that allows efficient random-access removal.
This deque is implemented as a doubly-linked list.
The links between elements are stored inside the elements themselves, allowing for efficient node searching.
Therefore, the elements must extend TrackedDeque.Element and can only be present in one TrackedDeque at a time.
An element cannot be present in one TrackedDeque multiple times.
This implementation does not support null elements. This implementation is not thread-safe.
- Since:
- 1.9
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the front of this deque.voidInserts the specified element at the end of this deque.voidclear()Removes all elements from this deque.booleanReturns if the deque contains the specified element.elementToFirstIterator(E startElement) Returns an iterator over the elements of this deque in order from last to first, starting at the specified element.elementToLastIterator(E startElement) Returns an iterator over the elements of this deque in order from first to last, starting at the specified element.Returns an iterator over the elements of this deque in order from first to last.voidCalls the provided action for each element in the deque, starting from the first element.booleanisEmpty()Returnstrueif this deque contains no elements.iterator()Returns an iterator over the elements of this deque in order from first to last.Returns an iterator over the elements of this deque in order from last to first.Returns the first element of the deque without removing it.peekLast()Returns the last element of the deque without removing it.booleanRemoves the specified element from this deque.Removes the first element of this deque and returns it.Removes the last element of this deque and returns it.longsize()Returns the number of elements in this deque.stream()Returns a stream of the elements in this deque.Returns a stream of the elements in this deque.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
spliterator
-
Constructor Details
-
TrackedDeque
public TrackedDeque()Creates a new and emptyTrackedDeque.
-
-
Method Details
-
addFirst
Inserts the specified element at the front of this deque.- Parameters:
e- The element to insert. Notnull.- Throws:
IllegalArgumentException- If the element already belongs to a TrackedDeque.NullPointerException- If the element isnull.- Implementation Note:
- O(1)
-
addLast
Inserts the specified element at the end of this deque.- Parameters:
e- The element to insert. Notnull.- Throws:
IllegalArgumentException- If the element already belongs to a TrackedDeque.NullPointerException- If the element isnull.- Implementation Note:
- O(1)
-
removeFirst
Removes the first element of this deque and returns it.- Returns:
- The removed element. If this deque is empty, returns
null. - Implementation Note:
- O(1)
-
removeLast
Removes the last element of this deque and returns it.- Returns:
- The removed element. If this deque is empty, returns
null. - Implementation Note:
- O(1)
-
remove
Removes the specified element from this deque.- Parameters:
e- The element to remove.- Returns:
- If the element was removed.
Returns
false, if the element isnullor not found in this deque. - Implementation Note:
- O(1)
-
peekFirst
Returns the first element of the deque without removing it.- Returns:
- The first element of the deque. If the deque is empty, returns null.
- Implementation Note:
- O(1)
-
peekLast
Returns the last element of the deque without removing it.- Returns:
- The last element of the deque. If the deque is empty, returns null.
- Implementation Note:
- O(1)
-
contains
Returns if the deque contains the specified element.- Parameters:
e- The element to check for.- Returns:
trueif the deque contains the specified element,falseotherwise.- Implementation Note:
- O(1)
-
clear
public void clear()Removes all elements from this deque.- Implementation Note:
- O(n)
-
size
public long size()Returns the number of elements in this deque.- Returns:
- The number of elements in this deque.
-
isEmpty
public boolean isEmpty()Returnstrueif this deque contains no elements.- Returns:
- If this deque contains no elements.
- Implementation Note:
- O(1)
-
forEach
Calls the provided action for each element in the deque, starting from the first element. If an action throws an exception, the iteration is immediately terminated and the exception is thrown.- Specified by:
forEachin interfaceIterable<E extends TrackedDeque.Element>- Parameters:
action- The action to be performed on each element. Notnull.- Throws:
ConcurrentModificationException- If the deque has been modified during the iteration.
-
iterator
Returns an iterator over the elements of this deque in order from first to last.The iterator supports removal. The iterator supports late-binding.
This method is identical to
firstToLastIterator().- Specified by:
iteratorin interfaceIterable<E extends TrackedDeque.Element>- Returns:
- The iterator. Not null.
-
firstToLastIterator
-
elementToLastIterator
Returns an iterator over the elements of this deque in order from first to last, starting at the specified element. All elements before the specified element are excluded from the iteration.The iterator supports removal.
- Parameters:
startElement- The starting element. Not null.- Returns:
- The iterator. Not null.
- Throws:
IllegalArgumentException- If the startElement is not contained in this deque.
-
lastToFirstIterator
-
elementToFirstIterator
Returns an iterator over the elements of this deque in order from last to first, starting at the specified element. All elements after the specified element are excluded from the iteration.The iterator supports removal.
- Parameters:
startElement- The starting element. Not null.- Returns:
- The iterator. Not null.
- Throws:
IllegalArgumentException- If the startElement is not contained in this deque.
-
stream
-
streamLastToFirst
-
toString
-