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:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Deque<E>
,java.util.List<E>
,java.util.Queue<E>
public class DoublyLinkedList<E extends DoublyLinkedList.Entry<E>> extends java.util.AbstractSequentialList<E> implements java.util.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 Classes Modifier and Type Class Description static class
DoublyLinkedList.Entry<E extends DoublyLinkedList.Entry<E>>
In order to maintain a doubly-linked list, each element needs to establish links to its adjacent elements.
-
Constructor Summary
Constructors Constructor Description DoublyLinkedList()
Constructs an empty list.DoublyLinkedList(java.util.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFirst(E e)
Inserts the specified element at the beginning of this list.void
addLast(E e)
Appends the specified element to the end of this list.boolean
contains(java.lang.Object o)
Returns true if this list contains the specified element.java.util.Iterator<E>
descendingIterator()
Returns an iterator over the elements in reverse sequential order.E
element()
Returns the first element in this list.E
getFirst()
Returns the first element in this list.E
getLast()
Returns the last element in this list.java.util.ListIterator<E>
listIterator(int i)
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.boolean
offer(E e)
Adds the specified element as the tail (last element) of this list.boolean
offerFirst(E e)
Inserts the specified element at the front of this list.boolean
offerLast(E e)
Inserts the specified element at the end of this list.E
peek()
E
peekFirst()
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.E
peekLast()
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.E
poll()
Retrieves and removes the head (first element) of this listE
pollFirst()
Retrieves and removes the first element of this list, or returns null if this list is empty.E
pollLast()
Retrieves and removes the last element of this list, or returns null if this list is empty.E
pop()
Pops an element from the stack represented by this list.void
push(E e)
Pushes an element onto the stack represented by this list.E
remove()
Retrieves and removes the head (first element) of this list.boolean
remove(java.lang.Object o)
Removes the first occurrence of the specified element from this list, if it is present.E
removeFirst()
Removes and returns the first element from this list.boolean
removeFirstOccurrence(java.lang.Object o)
Removes the first occurrence of the specified element in this list.E
removeLast()
Removes and returns the last element from this list.boolean
removeLastOccurrence(java.lang.Object o)
Removes the last occurrence of the specified element in this listint
size()
Returns the number of elements in this list.protected boolean
unlink(DoublyLinkedList.Entry<E> entry)
Called to remove children from the list.-
Methods inherited from class java.util.AbstractSequentialList
add, addAll, get, iterator, remove, set
-
Methods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
-
-
-
-
Constructor Detail
-
DoublyLinkedList
public DoublyLinkedList()
Constructs an empty list.
-
DoublyLinkedList
public DoublyLinkedList(java.util.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.- Parameters:
source
- the collection whose elements are to be placed into this list- Throws:
java.lang.NullPointerException
- if the specified collection is null
-
-
Method Detail
-
unlink
protected boolean unlink(DoublyLinkedList.Entry<E> entry)
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:
size
in interfacejava.util.Collection<E extends DoublyLinkedList.Entry<E>>
- Specified by:
size
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
size
in interfacejava.util.List<E extends DoublyLinkedList.Entry<E>>
- Specified by:
size
in classjava.util.AbstractCollection<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the number of elements in this list
-
addFirst
public void addFirst(E e)
Inserts the specified element at the beginning of this list.- Specified by:
addFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to add
-
addLast
public void addLast(E e)
Appends the specified element to the end of this list.- Specified by:
addLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to add
-
contains
public boolean contains(java.lang.Object o)
Returns true if this list contains the specified element.- Specified by:
contains
in interfacejava.util.Collection<E extends DoublyLinkedList.Entry<E>>
- Specified by:
contains
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
contains
in interfacejava.util.List<E extends DoublyLinkedList.Entry<E>>
- Overrides:
contains
in classjava.util.AbstractCollection<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
public E element()
Returns the first element in this list.- Specified by:
element
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
element
in interfacejava.util.Queue<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the first element in this list
- Throws:
java.util.NoSuchElementException
- if this list is empty
-
getFirst
public E getFirst()
Returns the first element in this list.- Specified by:
getFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the first element in this list
- Throws:
java.util.NoSuchElementException
- if this list is empty
-
getLast
public E getLast()
Returns the last element in this list.- Specified by:
getLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the last element in this list
- Throws:
java.util.NoSuchElementException
- if this list is empty
-
offer
public boolean offer(E e)
Adds the specified element as the tail (last element) of this list.- Specified by:
offer
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
offer
in interfacejava.util.Queue<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to add- Returns:
- true
-
offerFirst
public boolean offerFirst(E e)
Inserts the specified element at the front of this list.- Specified by:
offerFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to insert- Returns:
- true
-
offerLast
public boolean offerLast(E e)
Inserts the specified element at the end of this list.- Specified by:
offerLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to insert- Returns:
- true
-
peek
public E peek()
- Specified by:
peek
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
peek
in interfacejava.util.Queue<E extends DoublyLinkedList.Entry<E>>
-
peekFirst
public E peekFirst()
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.- Specified by:
peekFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the first element of this list, or null
-
peekLast
public E peekLast()
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.- Specified by:
peekLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the last element of this list, or null
-
poll
public E poll()
Retrieves and removes the head (first element) of this list- Specified by:
poll
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
poll
in interfacejava.util.Queue<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the head of this list, or null
-
pollFirst
public E pollFirst()
Retrieves and removes the first element of this list, or returns null if this list is empty.- Specified by:
pollFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the first element of this list, or null
-
pollLast
public E pollLast()
Retrieves and removes the last element of this list, or returns null if this list is empty.- Specified by:
pollLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the last element of this list, or null
-
pop
public E pop()
Pops an element from the stack represented by this list.- Specified by:
pop
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the element at the front of this list
- Throws:
java.util.NoSuchElementException
- if this list is empty.- See Also:
LinkedList.pop()
-
push
public void push(E e)
Pushes an element onto the stack represented by this list.- Specified by:
push
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Parameters:
e
- the element to push- See Also:
LinkedList.push(Object)
-
remove
public E remove()
Retrieves and removes the head (first element) of this list.- Specified by:
remove
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
remove
in interfacejava.util.Queue<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the head of this list
- Throws:
java.util.NoSuchElementException
- if this list is empty- See Also:
LinkedList.remove()
-
removeFirst
public E removeFirst()
Removes and returns the first element from this list.- Specified by:
removeFirst
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the first element from this list
- Throws:
java.util.NoSuchElementException
- if this list is empty- See Also:
LinkedList.removeFirst()
-
remove
public boolean remove(java.lang.Object o)
Removes the first occurrence of the specified element from this list, if it is present.- Specified by:
remove
in interfacejava.util.Collection<E extends DoublyLinkedList.Entry<E>>
- Specified by:
remove
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Specified by:
remove
in interfacejava.util.List<E extends DoublyLinkedList.Entry<E>>
- Overrides:
remove
in classjava.util.AbstractCollection<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:
LinkedList.remove(Object)
-
removeFirstOccurrence
public boolean removeFirstOccurrence(java.lang.Object o)
Removes the first occurrence of the specified element in this list.- Specified by:
removeFirstOccurrence
in interfacejava.util.Deque<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:
LinkedList.removeFirstOccurrence(Object)
-
removeLastOccurrence
public boolean removeLastOccurrence(java.lang.Object o)
Removes the last occurrence of the specified element in this list- Specified by:
removeLastOccurrence
in interfacejava.util.Deque<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:
LinkedList.removeLastOccurrence(Object)
-
removeLast
public E removeLast()
Removes and returns the last element from this list.- Specified by:
removeLast
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- the last element from this list
- Throws:
java.util.NoSuchElementException
- if this list is empty- See Also:
LinkedList.removeLast()
-
listIterator
@Nonnull public java.util.ListIterator<E> listIterator(int i)
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.- Specified by:
listIterator
in interfacejava.util.List<E extends DoublyLinkedList.Entry<E>>
- Specified by:
listIterator
in classjava.util.AbstractSequentialList<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:
java.lang.IndexOutOfBoundsException
- if the index is out of range.- See Also:
List.listIterator(int)
-
descendingIterator
@Nonnull public java.util.Iterator<E> 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:
descendingIterator
in interfacejava.util.Deque<E extends DoublyLinkedList.Entry<E>>
- Returns:
- an iterator over the elements in reverse sequence
- See Also:
LinkedList.descendingIterator()
-
-