Deque 方法
- /**
- * Inserts the specified element at the front of this list.
- *
- * @param e the element to insert
- * @return {@code true} (as specified by {@link Deque#offerFirst})
- * @since 1.6
- */
- public boolean offerFirst(E e) {
- addFirst(e);
- return true;
- }
- /**
- * Inserts the specified element at the end of this list.
- *
- * @param e the element to insert
- * @return {@code true} (as specified by {@link Deque#offerLast})
- * @since 1.6
- */
- public boolean offerLast(E e) {
- addLast(e);
- return true;
- }
- /**
- * Retrieves, but does not remove, the first element of this list,
- * or returns {@code null} if this list is empty.
- *
- * @return the first element of this list, or {@code null}
- * if this list is empty
- * @since 1.6
- */
- public E peekFirst() {
- final Node<E> f = first;
- return (f == null) ? null : f.item;
- }
- /**
- * Retrieves, but does not remove, the last element of this list,
- * or returns {@code null} if this list is empty.
- *
- * @return the last element of this list, or {@code null}
- * if this list is empty
- * @since 1.6
- */
- public E peekLast() {
- final Node<E> l = last;
- return (l == null) ? null : l.item;
- }
- /**
- * Retrieves and removes the first element of this list,
- * or returns {@code null} if this list is empty.
- *
- * @return the first element of this list, or {@code null} if
- * this list is empty
- * @since 1.6
- */
- public E pollFirst() {
- final Node<E> f = first;
- return (f == null) ? null : unlinkFirst(f);
- }
- /**
- * Retrieves and removes the last element of this list,
- * or returns {@code null} if this list is empty.
- *
- * @return the last element of this list, or {@code null} if
- * this list is empty
- * @since 1.6
- */
- public E pollLast() {
- final Node<E> l = last;
- return (l == null) ? null : unlinkLast(l);
- }
- /**
- * Pushes an element onto the stack represented by this list. In other
- * words, inserts the element at the front of this list.
- *
- * <p>This method is equivalent to {@link #addFirst}.
- *
- * @param e the element to push
- * @since 1.6
- */
- public void push(E e) {
- addFirst(e);
- }
- /**
- * Pops an element from the stack represented by this list. In other
- * words, removes and returns the first element of this list.
- *
- * <p>This method is equivalent to {@link #removeFirst()}.
- *
- * @return the element at the front of this list (which is the top
- * of the stack represented by this list)
- * @throws NoSuchElementException if this list is empty
- * @since 1.6
- */
- public E pop() {
- return removeFirst();
- }
- /**
- * Removes the first occurrence of the specified element in this
- * list (when traversing the list from head to tail). If the list
- * does not contain the element, it is unchanged.
- *
- * @param o element to be removed from this list, if present
- * @return {@code true} if the list contained the specified element
- * @since 1.6
- */
- public boolean removeFirstOccurrence(Object o) {
- return remove(o);
- }
- /**
- * Removes the last occurrence of the specified element in this
- * list (when traversing the list from head to tail). If the list
- * does not contain the element, it is unchanged.
- *
- * @param o element to be removed from this list, if present
- * @return {@code true} if the list contained the specified element
- * @since 1.6
- */
- public boolean removeLastOccurrence(Object o) {
- if (o == null) {
- for (Node<E> x = last; x != null; x = x.prev) {
- if (x.item == null) {
- unlink(x);
- return true;
- }
- }
- } else {
- for (Node<E> x = last; x != null; x = x.prev) {
- if (o.equals(x.item)) {
- unlink(x);
- return true;
- }
- }
- }
- return false;
- }
复制代码
------
原文链接:https://pdai.tech/md/java/collection/java-collection-LinkedList.html
|