Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java LinkedList - differences between retrieve operations

Are there any differences between different methods in each of the following groups of element retrieve operations in LinkedList?

Returning null + removing operations: poll(), pollFirst().

Returning null + not removing operations: peek(), peekFirst().

Throwing exception + removing operations: pop(), remove(), removeFirst().

Throwing exception + not removing operations: element(), getFirst().

Similar duplications exists in insertion methods.

If there is no such difference, I would expect it to be mentioned in the javadoc of the methods (something like the good old "This is exactly like calling ..."). Is it only a sloppy documentation, or am I missing anything?

like image 406
Elist Avatar asked Feb 13 '13 10:02

Elist


People also ask

Which operations will be much faster for LinkedList than ArrayList?

ArrayList is faster in storing and accessing data. LinkedList is faster in manipulation of data.

What is retrieval in LinkedList?

Retrieval of elements in LinkedList is very slow compared to ArrayList. Because to retrieve an element, you have to traverse from beginning or end (Whichever is closer to that element) to reach that element. Retrieval operation in ArrayList is of order of O(1). Retrieval operation in LinkedList is of order of O(n).

Why retrieval is faster in ArrayList?

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

Is it faster to remove items from a LinkedList or an ArrayList?

LinkedList is faster than ArrayList while inserting and deleting elements, but it is slow while fetching each element.


2 Answers

There is no difference between them, and it is listed in the documentation too, but you have to do some recursive searching to get there.

LinkedList implements two interfaces - Queue and Deque. And Deque extends from Queue.

Now, Deque has defined the method - Deque#pollFirst() and inherited the method - Queue#poll().

So, LinkedList has basically these two methods defined for the two interfaces it implements.

And about the similarity between those two methods, it is listed in documentation of Deque as:

This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. Elements are added at the end of the deque and removed from the beginning. The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:

And there is a table listing the methods of Queue class and the equivalent Deque method. See Deque#poll(), Deque#peek() for e.g. They clearly list the equivalent method.

like image 181
Rohit Jain Avatar answered Oct 18 '22 22:10

Rohit Jain


The difference between them is version they were released with and the interfaces that LinkedList implements.

Example based on poll() and pollFirst():

LinkedList was released along with Java 1.2.

Since 1.5 LinkedList implements the Queue interface, which has

public E poll() 

Since 1.6 LinkedList implements the Deque interface, which has

public E pollFirst()

edit: It is important to keep the older implementation due to a backward compatibility.

like image 1
BartoszMiller Avatar answered Oct 18 '22 21:10

BartoszMiller