Deque

If Java had true garbage collection, most programs would delete themselves upon execution. Robert Sewell

LinkedList

Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-22 19:26:48

0 Comments


Definition

Linked list is a Doubly-linked list implementation. Linked list is a data structure consisting of a group of nodes which together represent a sequence which each node link with previous and next node.

Character

Doubly-linked list implementation of the List and Deque.
Not synchronized. If the linked list accesses from multiple threads, external synchronization required.
Fail-fast of an iterator is not guaranteed.
If multiple threads access and try to update, It throws ConcurrentModificationException.

source Code

								
package com.careerdrill.collections.deque;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class LinkedListUse {
	
	public static void main(String[] args) {

	int []array={12,24,48,78,99};
		
    System.out.println("Add the elements to the linked list: "+Arrays.toString(array));
    List< Integer > ll = new LinkedList< Integer >();

    for(int i : array)
    	ll.add(i);

    System.out.println("Add '11' at end of the linked list");
    ((LinkedList< Integer >) ll).addLast(11);
    
    System.out.println("Add '9' at beginning  of the linked list");
    ((LinkedList< Integer >) ll).addFirst(9);
    ll.add(1, 22);
    
    System.out.println("Display all the linked list elements: " + ll);
	}
    
}
						

Output

    					 
Add the elements to the linked list: [12, 24, 48, 78, 99]
Add '11' at end of the linked list
Add '9' at beginning of the linked list
Display all the linked list elements: [9, 22, 12, 24, 48, 78, 99, 11]
    					 
					    

Comparison

linkedlist Array List
Linked list search operation is slower than array list. ArrayList search operation is pretty fast compared to the LinkedList.
LinkedList element addition and deletion is faster compared to ArrayList. Array List takes more time for inserting and deleting the elements.
LinkedList has more memory overhead than ArrayList. ArrayList has less memory overhead compared than LinkedList.
Internally uses dynamic array. Internally uses doubly linked list.
Linked list uses links and no need adjust index. ArrayList needs to adjust the indexes according to deletion or insertion.

When to use

Linked list is the best choice when the user uses already defined iterators and insert/delete the elements at beginning and end.

0 Comments



Please login to add comments.