Package com.linkedin.alpini.base.misc
Class DoublyLinkedList<E extends DoublyLinkedList.Entry<E>>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.AbstractSequentialList<E>
com.linkedin.alpini.base.misc.DoublyLinkedList<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Deque<E>,List<E>,Queue<E>
public class DoublyLinkedList<E extends DoublyLinkedList.Entry<E>>
extends AbstractSequentialList<E>
implements Deque<E>
Linked list implementation of the List interface.
Implements all optional list operations, but only permits elements of the DoublyLinkedList.Entry class.
In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get,
remove and insert an element at the beginning and end of the list.
These operations allow linked lists to be used as a stack, queue, or double-ended queue.
The class implements the Deque interface, providing first-in-first-out queue operations for add, poll,
along with other stack and deque operations.
All of the operations perform as could be expected for a doubly-linked list.
Operations that index into the list will traverse the list from the beginning or the end,
whichever is closer to the specified index.
Note that this implementation is not synchronized. If multiple threads access a linked list concurrently,
and at least one of the threads modifies the list structurally, it must be synchronized externally.
(A structural modification is any operation that adds or deletes one or more elements;
merely setting the value of an element is not a structural modification.)
This is typically accomplished by synchronizing on some object that naturally encapsulates the list.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDoublyLinkedList.Entry<E extends DoublyLinkedList.Entry<E>>In order to maintain a doubly-linked list, each element needs to establish links to its adjacent elements. -
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty list.DoublyLinkedList(Collection<? extends E> source) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the beginning of this list.voidAppends the specified element to the end of this list.booleanReturns true if this list contains the specified element.Returns an iterator over the elements in reverse sequential order.element()Returns the first element in this list.getFirst()Returns the first element in this list.getLast()Returns the last element in this list.listIterator(int i) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.booleanAdds the specified element as the tail (last element) of this list.booleanofferFirst(E e) Inserts the specified element at the front of this list.booleanInserts the specified element at the end of this list.peek()Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.peekLast()Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.poll()Retrieves and removes the head (first element) of this listRetrieves and removes the first element of this list, or returns null if this list is empty.pollLast()Retrieves and removes the last element of this list, or returns null if this list is empty.pop()Pops an element from the stack represented by this list.voidPushes an element onto the stack represented by this list.remove()Retrieves and removes the head (first element) of this list.booleanRemoves the first occurrence of the specified element from this list, if it is present.Removes and returns the first element from this list.booleanRemoves the first occurrence of the specified element in this list.Removes and returns the last element from this list.booleanRemoves the last occurrence of the specified element in this listintsize()Returns the number of elements in this list.protected booleanunlink(DoublyLinkedList.Entry<E> entry) Called to remove children from the list.Methods inherited from class java.util.AbstractSequentialList
add, addAll, get, iterator, remove, setMethods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toStringMethods 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, containsAll, isEmpty, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
Constructor Details
-
DoublyLinkedList
public DoublyLinkedList()Constructs an empty list. -
DoublyLinkedList
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
source- the collection whose elements are to be placed into this list- Throws:
NullPointerException- if the specified collection is null
-
-
Method Details
-
unlink
Called to remove children from the list.- Parameters:
entry- child entry.- Returns:
- true if successful.
-
size
public int size()Returns the number of elements in this list.- Specified by:
sizein interfaceCollection<E extends DoublyLinkedList.Entry<E>>- Specified by:
sizein interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
sizein interfaceList<E extends DoublyLinkedList.Entry<E>>- Specified by:
sizein classAbstractCollection<E extends DoublyLinkedList.Entry<E>>- Returns:
- the number of elements in this list
-
addFirst
Inserts the specified element at the beginning of this list.- Specified by:
addFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to add
-
addLast
Appends the specified element to the end of this list.- Specified by:
addLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to add
-
contains
Returns true if this list contains the specified element.- Specified by:
containsin interfaceCollection<E extends DoublyLinkedList.Entry<E>>- Specified by:
containsin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
containsin interfaceList<E extends DoublyLinkedList.Entry<E>>- Overrides:
containsin classAbstractCollection<E extends DoublyLinkedList.Entry<E>>- Parameters:
o- element whose presence in this list is to be tested- Returns:
- if this list contains the specified element
-
element
Returns the first element in this list.- Specified by:
elementin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
elementin interfaceQueue<E extends DoublyLinkedList.Entry<E>>- Returns:
- the first element in this list
- Throws:
NoSuchElementException- if this list is empty
-
getFirst
Returns the first element in this list.- Specified by:
getFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the first element in this list
- Throws:
NoSuchElementException- if this list is empty
-
getLast
Returns the last element in this list.- Specified by:
getLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the last element in this list
- Throws:
NoSuchElementException- if this list is empty
-
offer
Adds the specified element as the tail (last element) of this list.- Specified by:
offerin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
offerin interfaceQueue<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to add- Returns:
- true
-
offerFirst
Inserts the specified element at the front of this list.- Specified by:
offerFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to insert- Returns:
- true
-
offerLast
Inserts the specified element at the end of this list.- Specified by:
offerLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to insert- Returns:
- true
-
peek
- Specified by:
peekin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
peekin interfaceQueue<E extends DoublyLinkedList.Entry<E>>
-
peekFirst
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.- Specified by:
peekFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the first element of this list, or null
-
peekLast
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.- Specified by:
peekLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the last element of this list, or null
-
poll
Retrieves and removes the head (first element) of this list- Specified by:
pollin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
pollin interfaceQueue<E extends DoublyLinkedList.Entry<E>>- Returns:
- the head of this list, or null
-
pollFirst
Retrieves and removes the first element of this list, or returns null if this list is empty.- Specified by:
pollFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the first element of this list, or null
-
pollLast
Retrieves and removes the last element of this list, or returns null if this list is empty.- Specified by:
pollLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the last element of this list, or null
-
pop
Pops an element from the stack represented by this list.- Specified by:
popin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the element at the front of this list
- Throws:
NoSuchElementException- if this list is empty.- See Also:
-
push
Pushes an element onto the stack represented by this list.- Specified by:
pushin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
e- the element to push- See Also:
-
remove
Retrieves and removes the head (first element) of this list.- Specified by:
removein interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
removein interfaceQueue<E extends DoublyLinkedList.Entry<E>>- Returns:
- the head of this list
- Throws:
NoSuchElementException- if this list is empty- See Also:
-
removeFirst
Removes and returns the first element from this list.- Specified by:
removeFirstin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the first element from this list
- Throws:
NoSuchElementException- if this list is empty- See Also:
-
remove
Removes the first occurrence of the specified element from this list, if it is present.- Specified by:
removein interfaceCollection<E extends DoublyLinkedList.Entry<E>>- Specified by:
removein interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Specified by:
removein interfaceList<E extends DoublyLinkedList.Entry<E>>- Overrides:
removein classAbstractCollection<E extends DoublyLinkedList.Entry<E>>- Parameters:
o- element to be removed from this list,- Returns:
- true if this list contained the specified element
- See Also:
-
removeFirstOccurrence
Removes the first occurrence of the specified element in this list.- Specified by:
removeFirstOccurrencein interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
o- element to be removed from this list, if present- Returns:
- true if the list contained the specified element
- See Also:
-
removeLastOccurrence
Removes the last occurrence of the specified element in this list- Specified by:
removeLastOccurrencein interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Parameters:
o- element to be removed from this list, if present- Returns:
- true if the list contained the specified element
- See Also:
-
removeLast
Removes and returns the last element from this list.- Specified by:
removeLastin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- the last element from this list
- Throws:
NoSuchElementException- if this list is empty- See Also:
-
listIterator
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.- Specified by:
listIteratorin interfaceList<E extends DoublyLinkedList.Entry<E>>- Specified by:
listIteratorin classAbstractSequentialList<E extends DoublyLinkedList.Entry<E>>- Parameters:
i- index of the first element to be returned from the list-iterator (by a call to next)- Returns:
- a ListIterator of the elements in this list
- Throws:
IndexOutOfBoundsException- if the index is out of range.- See Also:
-
descendingIterator
Returns an iterator over the elements in reverse sequential order. The elements will be returned in order from last (tail) to first (head).- Specified by:
descendingIteratorin interfaceDeque<E extends DoublyLinkedList.Entry<E>>- Returns:
- an iterator over the elements in reverse sequence
- See Also:
-