How to Java programming- Java Linkedlist

In this tutorial, we will see an example of LinkedList along with brief description of its methods. Java LinkedList class uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces. Java LinkedList class can contain duplicate elements and it maintains insertion order.
Java LinkedList class is non-synchronized and can be used as a list, stack or queue. In Java LinkedList class, manipulation is fast because no shifting needs to occur.

LinkedList
	package LinkedListPackage;
	import java.util.LinkedList;

	public class LinkedListExample {
	  public static void main(String a[]){
		LinkedList<String> ll = new LinkedList<String>();
		ll.add("Orange");
		ll.add("Apple");
		ll.add("Grape");
		ll.add("Banana");
		System.out.println(ll);
		System.out.println("Size of the linked list: "+ll.size());
		System.out.println("Is LinkedList empty? "+ll.isEmpty());
		System.out.println("Does LinkedList contains 'Grape'? "+ll.contains("Grape"));
	  }
	}

 

Output
     [Orange, Apple, Grape, Banana] 
     Size of the linked list: 4 
     Is LinkedList empty? false 
     Does LinkedList contains 'Grape'? true

LinkedList defines many methods − See the Documentation

 

Methods of ArrayList class.

LinkedList MethodsDescription
boolean add(Object item)It adds the item at the end of the list.
void add(int index, Object item)It adds an item at the given index of the the list.
boolean addAll(Collection c)It adds all the elements of the specified collection c to the list.
boolean addAll(int index, Collection c) It adds all the elements of collection c to the list starting from a give index in the list.
void addFirst(Object item)It adds the item (or element) at the first position in the list.
void addLast(Object item)It inserts the specified item at the end of the list.
void clear()It removes all the elements of a list.
Object clone()It returns the copy of the list.
boolean contains(Object item)It checks whether the given item is present in the list or not.
Object get(int index)It returns the item of the specified index from the list.
Object getFirst()It fetches the first item from the list.
Object getLast()It fetches the last item from the list.
int indexOf(Object item)It returns the index of the specified item.
int lastIndexOf(Object item)It returns the index of last occurrence of the specified element.
Object poll()It returns and removes the first item of the list.
Object pollFirst()same as poll() method. Removes the first item of the list.
Object pollLast()It returns and removes the last element of the list.
Object remove()It removes the first element of the list.
Object remove(int index)It removes the item from the list which is present at the specified index.
Object remove(Object obj)It removes the specified object from the list.
Object removeFirst()It removes the first item from the list.
Object removeLast()It removes the last item of the list.
Object removeFirstOccurrence(Object item) It removes the first occurrence of the specified item.
Object removeLastOccurrence(Object item)It removes the last occurrence of the given element.
Object set(int index, Object item) It updates the item of specified index with the give value.
int size()It returns the number of elements of the list.

 

Difference between ArrayList and LinkedList

ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non-synchronized classes.

ArrayListLinkedList
ArrayList internally uses dynamic array to store the elements.LinkedList internally uses doubly linked list to store the elements.
Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory.
ArrayList class can act as a list only because it implements List only.LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
ArrayList is better for storing and accessing dataLinkedList is better for manipulating data.

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...