Java Collections Tutorial 04 | LinkedList in Java | LinkedList |Java9s.com

preview_player
Показать описание
LinkedList is an Ordered collection. It is an interesting implementation of List and it has some extra functionality compared to ArrayList and Vector.

So, if you take a look at the hierarchy of LinkedList, you can see that it is from List and it is also implmenting the Queue and Double ended queue interfaces.
So, most of the methods from List have been discussed in ArrayList module. We will focus on the queue and double ended queue features of LinkedList.
So, what is the difference between the normal list based implementations like ArrayList, Vector vs LinkedList.

With ArrayList or Vector, when we add an element without specifying an index, they place that element at the end of the list by default.

AddFirst method adds element to the beginning of List
AddLast method adds the element at the end of the List
getFirst method returns the first and getLast returns the last element in the list
removeFirst and removeLast methods remove the first and last elements respectively.
With LinkedList, because it is a Doubled ended queue, we can add the element to the front or at the end of the list.
To enable this, LinkedList implements a range of methods to be able to add, remove and get elements from front or from end of the collection.

So, LinkedList implementing the Dqueue interface is like double edged sword. If you think the element you want can be reached from end of collection, we can use this or if from the first, we then have normal iterator.
Рекомендации по теме
Комментарии
Автор

Great tutorials helps a lot to survive in IT industry

sumitgoyal
Автор

If anyone wants the assignment, comment here :)

mike
Автор

I had read online that the below piece of code will throw an Exception, if what we are attempting to do is, to remove an element while iterating.

for(Integer i: list) {
list.remove(i);
}

The exception was given as


Could you help me understand his code?

mani
Автор

Please let me know how linked list improves the retrieval performance ?

greatkk
Автор

sir, what is the difference between remove and poll?? and can I use both list and queye method together in linkedlist?

shahriarhaqueabir
Автор

Can you please share me the solution for the assignment?

avirenis
Автор

whats the difference between addfirst and offerfirst? addlast and offerlast? removefirst, poll and poll first? etc. There are alot of similar methods here, whats the point if they all accomplish the same task?

Stonecoldsteverock