Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does LinkedList work internally in Java?

As far as I know, the concept of a linked list is a bunch of object connected to each other by having a 'next' and sometimes 'previous' attribute to traverse the objects.

I noticed in Java, you can create a LinkedList object...but treat it like an array/list/sequence by using the same methods such as .add(), .get(), etc.

So, is LinkedList internally an array-like sequence?

like image 787
sqram Avatar asked Nov 23 '11 08:11

sqram


6 Answers

So, is LinkedList internally an array-like sequence?

No. It's a series of instances of a private nested class Entry, which has next, previous and element references. Note that you could have found this out yourself by looking at the source code, which comes with the JDK.

The reason why this internal structure is not exposed is that it prevents the structure from becoming corrupted and e.g. containing a loop. And the uniform access via the List and Deque interfaces allows polymorphic use.

like image 65
Michael Borgwardt Avatar answered Oct 19 '22 05:10

Michael Borgwardt


A LinkedList in Java works just as you would expect it to do. If you use the official Collections LinkedList then it will indeed be a a bunch of object connected to each other by having a 'next' and sometimes 'previous'.

And yes, it has a get(int index) method which is surprising because it would not be very efficient as you would need to start at the beginning and count your way up the list to find the indexth entry and this is not what LinkedLists are good at. The reason it is there is because LinkedList implements the List interface. This is something you can with ALL lists.

However, you would probably try to avoid using a LinkedList when most of your access to it is through the get(int index) method as that would clearly be most inefficient. You would be perhaps better to use an ArrayList.

like image 37
OldCurmudgeon Avatar answered Oct 19 '22 05:10

OldCurmudgeon


LinkedList is a chain of entities in which every entity knows about next-one, so get(index) operation requires iterating over this chain with counter. But this list optimized for adding and deleting by position (in case when I need to put element inside list or delete element from middle linked list performs better)

like image 44
Stanislav Levental Avatar answered Oct 19 '22 06:10

Stanislav Levental


as i know linkedlist is like what you said in the first paragraph. each object has two references, called it's previous and it's next. unlike array which works sequentially (in C it's exactly stored in sequence. i think java just does the same, that's why we can't re-size an array), list works by referencing. so logically it works slower than array does.

like image 33
simaremare Avatar answered Oct 19 '22 04:10

simaremare


It is possible to implement an object that works like an array, using a linked list. Compared to arrays, some operations will be faster and some will be slower. For example, calling get() for an element near the end might be quite a lot slower with a linked list than with an array. But, it's still possible.

On the other hand, deleting an element from the middle of a linked list will be faster than the corresponding operation done using arrays.

like image 35
Greg Hewgill Avatar answered Oct 19 '22 05:10

Greg Hewgill


Not really. Linked list is provided by the collection and by good design you could create a double linked list. Now it is not like array because these objects will not have indexes and are not elements within the container i.e. list. Hence you go through defining the next and previous and dictate what the next move is. They all have the same methods because again they are collections like i said.

like image 30
sys_debug Avatar answered Oct 19 '22 04:10

sys_debug