通过api文档发现LinkedList比ArrayList多个 removeFirst()和 removeLast()方法。
既然有了 remove()为何还要多造其他俩种??
看源码
LinkedList
/**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
* @since 1.5
*/
public E remove() {
return removeFirst();
}
/**
* Removes and returns the first element from this list.
*
* @return the first element from this list
* @throws NoSuchElementException if this list is empty
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
原来LinkedList的remove()就是removeFirst(),而接口 Collection是没有无参的remove(),所以这是它特有的方法,那为啥要多此一举呢?
个人认为链表结构的删除需要操作前后节点指向,但第一个没有没必要多此一举所以用了简化版的删除,而且也大大提高了遍历删集合的效率(ArrayList的我不想吐槽)