Tutorials Hut

Java LinkedList 

Java LinkedList is a class which extends AbstractSequentialList which implements List interface. LinkedList can store non-primitive data types as elements and form a list. LinkedList is ordered list that means elements are arranged in same order they are added.

·  LinkedList may contain duplicate elements.

·  LinkedList is not synchronized.

·  LinkedList manipulation is faster than ArrayList as we can easily remove by element by breaking from link.

LinkedList Declaration

Non Generic Style

LinkedList myList = new LinkedList();

Generic Style

LinkedList  myList = new LinkedList ();

LinkedList Constructors

Constructor Description
LinkedList( Creates empty linked list
LinkedList(Collection<? extends E> c) ArrayList initialized with some collection.

LinkedList Methods

Method Description
boolean add(E e) Ads an element
void add(int index, E element) Ads an element at an index
boolean addAll(Collection<? extends E> c) Adds a collection.
boolean addAll(Collection<? extends E> c) Adds a collection.
boolean addAll(int index, Collection<? extends E> c) Adds a collection at an index.
void addFirst(E e) Adds an element at start of list.
void addLast(E e) Adds an element at the end of list.
void clear() Clears.
Object clone() Creates clone of linked list.
boolean contains(Object o) Checks if objects is present in linkedlist.
Iterator descendingIterator() Returns iterator.
E element() Returns first element.
E get(int index) Returns element at given index.
E getFirst() Returns first element.
E getLast() Returns last element.
int indexOf(Object o) Returns index of given object in list.
int lastIndexOf(Object o) Returns last index of given object in linked list.
ListIterator listIterator(int index) Returns list iterator.
boolean offer(E e) Adds element as last element.
boolean offerFirst(E e) Adds element at first position of list.
boolean offerLast(E e) Adds element at last position of list.
E peek() Retruns first element.
E peekFirst() Returns first element.
E peekLast() Returns last element or null if not present.
E poll() Removes first element.
E pollFirst() Removes and returns first element.
E pollLast() Removes and returns last element.
E pop() It pops first element of stack represented by list.
void push(E e) It pushes an element on stack represented by list.
E remove() Removes element.
E remove(int index) Removes element at given index.
boolean remove(Object o) Removes given object from list.
E removeFirst() Removes first element.
boolean removeFirstOccurrence(Object o) Removes first occurrence.
E removeLast() Removes last element.
boolean removeLastOccurrence(Object o) Removes last occurance.
E set(int index, E element) Replaces element at given index.
Object[] toArray() Returns array of list
T[] toArray(T[] a) Returns array of list
int size() Returns size of linkedlist.

LinkedList Example With Iterating Using Iterator()

import java.util.*;
class CollectionsExample3{ public static void main(String args[]){ //create list LinkedList list=new LinkedList(); //add elements in list list.add(“Tom”); list.add(“Bob”); Iterator itr=list.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } } Output

Iterating  LinkedList or collection with forEach, for, ListIterator  and forEachRemaining

import java.util.LinkedList;
import java.util.ListIterator;
public class CollectionsExample3 { public static void main(String[] args) { LinkedList myList=new LinkedList<>(); myList.add(“John”); myList.add(“Ron”); //foreach myList.forEach(data->System.out.println(“forEach: “+data)); //for for (String data : myList) { System.out.println(“for: “+data); } //list itr ListIterator ltr = myList.listIterator(); while(ltr.hasNext()) { System.out.println(“listIterator: “+ltr.next()); } //forEachRemaining of iterator ltr.forEachRemaining(data->{ System.out.println(“forEachRemaining: “+data); }); } } Output:
forEach: John
forEach: Ron
for: John
for: Ron
listIterator: John
listIterator: Ron

LinkedList/Collections examples of add, addAll, remove, removeAll and retainAll()

import java.util.LinkedList;
public class CollectionsExample3 {
public static void main(String[] args) {
LinkedList myList=new LinkedList&lt;&gt;();
LinkedList myList2=new LinkedList&lt;&gt;();






[John, Ron, Jone, Tony]
[Ron, Jone]

Sorting the LinkedList

We can use collections class’ sorting methods to sort it. Syntax: Collections.sort();

Next Article

  •  Set and HashSet

  • Leave a Reply

    Your email address will not be published. Required fields are marked *