Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why LinkedList doesn't have initialCapacity in java?

I wonder why LinkedList doesn't have initialCapacity.

I know good when to use ArrayList and when LinkedList.

Its good practice to define Collection final size like:

List<String> arraylist = new ArrayList<String>(5);

For LinkedList for example:

List<String> linkedlist = new LinkedList<String>(); // right way

but

List<String> arraylist = new LinkedList<String>(5); // compilation error

Can somebody spread a light on that issue?

[EDIT]

BTW, I can write

List<String> arraylist = new ArrayList<String>(5);
List<String> linkedlist = new LinkedList<String>(arraylist);
like image 950
Maxim Shoustin Avatar asked Sep 27 '13 11:09

Maxim Shoustin


People also ask

Why LinkedList is not synchronized?

LinkedList Features Permits all elements including duplicates and NULL. LinkedList maintains the insertion order of the elements. It is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

Does LinkedList allow null values in Java?

Null Elements:LinkedList allow any number of null values while LinkedHashSet also allows maximum one null element.

Does LinkedList have capacity?

By default, an creates a list of initial capacity 10, while LinkedList only constructs an empty list without any initial capacity.

Does LinkedList follow insertion order?

Java LinkedList maintains the insertion order of the elements.


6 Answers

LinkedList by nature does not have "capacity", since it does not allocate memory to the items before the items are added to the list. Each item in a LinkedList holds a pointer to the next in the list.

http://www.stoimen.com/blog/wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list.png

There would be no point in allocating memory to the list beforehand, since LinkedList does not have capacity.

like image 53
Kimi Avatar answered Oct 13 '22 10:10

Kimi


Its model is not based on an array but rather a true linked list, and so there is no need and further it would not make sense. It doesn't make much sense to have empty links like you have empty array items.

like image 36
Hovercraft Full Of Eels Avatar answered Oct 13 '22 09:10

Hovercraft Full Of Eels


Why would you need a capacity on a LinkedList? A LinkedList does not work with fixed sized arrays. Every LinkedListElement has a pointer (a link!) to the next Element in the list. Which Because of that it is possible to add an element to a linked list in constant time. But it is costly to have random access to the elements in the List. You need to go through all the Elements in the list until you reach your destination.

like image 24
Carsten Hoffmann Avatar answered Oct 13 '22 08:10

Carsten Hoffmann


Why would LinkedList have an initial capacity?

ArrayList is backed up by an array, so the initial capacity is the initial size of the array. LinkedList has no need of that.

like image 43
Gabriel Negut Avatar answered Oct 13 '22 09:10

Gabriel Negut


Linkedlist does not need an initial value. Thats is the primary difference between array and linked list.

array will end somewhere. But linkedlist not. Linked list does not work on boundary values.

like image 23
Arjun Avatar answered Oct 13 '22 08:10

Arjun


When you declare an array you have to know its size because pointers need to be created in memory. A linked list does not need this because there is no need for pointers to memory before any object is added to the list.

A linked list is defined recursively as: an empty list en element that points to the empty list

therefore whenever you add an element, you allocate memory (or rather in Java the compiler does this) when you create the element, and then when you add it to the list it now points to the list (or the last element in the list points to it).

So you don't need to declare initial size of linked list because a linked list always starts with the empty list, and when an element is added it points to the list.

like image 21
Arash Saidi Avatar answered Oct 13 '22 08:10

Arash Saidi