Tutorials Hut

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
Tom
Bob

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

Example:
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;();

//add
myList.add("John");
myList.add("Ron");
myList.add("Jone");
myList.add("Tony");

myList2.add("Tony");
System.out.println(myList);

//addAll
myList.addAll(myList2);

//remove
myList.remove("John");

//removeAll
myList.removeAll(myList2);
System.out.println(myList);

//retainAll
myList.add("Tony");
myList.retainAll(myList2);
System.out.println(myList);
}
}
Output
[John, Ron, Jone, Tony]
[Ron, Jone]
[Tony]

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 *

    Scroll to Top