• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » LinkedList Class In Java

LinkedList Class In Java

Last Updated on: October 20, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Important Methods Of LinkedList
  • Constructors for Java LinkedList
  • LinkedList Class Methods
  • How to Reverse LinkedList Value Using Collections In Java With Example?
  • Different Ways to Remove Elements LinkedList In Java With Example?
  • How To Add Values Last LinkedList in Java With Example?

LinkedList Class In Java: A LinkedList has a group of elements in the form of nodes. Each node has the three fields where the data field contains the data and the link fields contain a reference to the previous and next nodes.

The Java LinkedList class implements the list interface.

LinkedList is very convenient to store the data. Inserting the elements into the LinkedList and removing the elements from the LinkedList is done quickly; that’s why it is preferred over the arrays.

LinkedList also has some disadvantages like the nodes can not be directly like in array we are accessing an element by using its index. Here we need to start from the head and follow through the link to reach to a node we which to access.

To store the elements in a linked list, we use a double linked list which provides a linear data structure and also used to inherit an abstract class and implement list and deque interfaces.

Important Methods Of LinkedList

  • It can contain duplicate elements.
  • The insertion is preserved.
  • Java LinkedList class ios non synchronised.
  • Null Insertion is possible
  • Duplicate and Heterogeneous elements are allowed
  • LinkedList implements the Serializable and Cloneable interfaces but not the random access interface.
  • LinkedList is the best choice if you are frequent operation is insertion and deletion in the middle, and if you frequent operation is retrieval, then don’t use LinkedList that time you can use ArrayList.
  • The underline data structure for LinkedList is Double linked list.

Constructors for Java LinkedList

The LinkedList class also have various methods and constructors like other java collections. The LinkedList class have two constructors that are:

  • LinkedList(): Used to create an empty linked list.
  • LinkedList(Collection C): Used to create an ordered list which contains all the elements of a specified collection, as returned by the collection’s iterator.

LinkedList Class Methods

How to Verify Element Presence LinkedList Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
import java.util.List;
public class VerifyLinkedListElement {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      LinkedList<String> arrl = new LinkedList<String>();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        List<String> list = new LinkedList<String>();
        list.add("Second");
        list.add("Random");
        System.out.println("Does LinkedList contains all list elements?: "+arrl.containsAll(list));
        list.add("one");
        System.out.println("Does LinkedList contains all list elements?: "+arrl.containsAll(list));
   }
}

Output:

Does LinkedList contains all list elements?: true
Does LinkedList contains all list elements?: false

How To Get All Values LinkedList HasNext() In Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
public class LastElementLinkedList {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      LinkedList<String> arrl = new LinkedList<String>();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Last Element: "+arrl.getLast());
        System.out.println("Last Element: "+arrl.peekLast());
   }
}

Output:

Last Element: Random
Last Element: Random

Write a Program to Get the First Element LinkedList Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
public class GetFirstElementLinkedListJava {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   LinkedList<String> arrl = new LinkedList<String>();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("First Element: "+arrl.element());
        System.out.println("First Element: "+arrl.getFirst());
        System.out.println("First Element: "+arrl.peek());
        System.out.println("First Element: "+arrl.peekFirst());
   }
}

Output:

First Element: First
First Element: First
First Element: First
First Element: First

Write a Program to Clear LinkedList Values In Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
public class ClearMyLinkedList {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      LinkedList<String> arrl = new LinkedList<String>();
      //adding elements to the end
      arrl.add("First");
      arrl.add("Second");
      arrl.add("Third");
      arrl.add("Random");
      System.out.println("Actual LinkedList:"+arrl);
      arrl.clear();
      System.out.println("After clear LinkedList:"+arrl);
   }
}

Output:

Actual LinkedList:[First, Second, Third, Random]
After clear LinkedList:[]

How to Reverse LinkedList Value Using Collections In Java With Example?

package com.java.Softwaretestingblog;
import java.util.Collections;
import java.util.LinkedList;
public class Reverse_Value_LinkedList {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   LinkedList<String> list = new LinkedList<String>();
        list.add("Java");
        list.add("Cric");
        list.add("Play");
        list.add("Watch");
        list.add("Glass");
        Collections.reverse(list);
        System.out.println("Results after reverse operation:");
        for(String str: list)
        {
            System.out.println(str);
        }
   }
}

Output:

Results before reverse operation:
Java
Cric
Play
Watch
Glass
Results after reverse operation:
Glass
Watch
Play
Cric
Java

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Different Ways to Remove Elements LinkedList In Java With Example?

package com.softwaretestingo.java.basics;
import java.util.LinkedList;
public class RemoveFromLinkedList 
{
   public static void main(String[] args) 
   {
      LinkedList<String> arrl = new LinkedList<String>();
      arrl.add("First");
      arrl.add("Second");
      arrl.add("Third");
      arrl.add("Random");
      arrl.add("four");
      arrl.add("five");
      arrl.add("six");
      arrl.add("seven");
      arrl.add("eight");
      arrl.add("nine");
      System.out.println(arrl);
      System.out.println("Remov() method:"+arrl.remove());
      System.out.println("After remove() method call:");
      System.out.println(arrl);
      System.out.println("remove(index) method:"+arrl.remove(2));
      System.out.println("After remove(index) method call:");
      System.out.println(arrl);
      System.out.println("Remov(object) method:"+arrl.remove("six"));
      System.out.println("After remove(object) method call:");
      System.out.println(arrl);
      System.out.println("removeFirst() method:"+arrl.removeFirst());
      System.out.println("After removeFirst() method call:");
      System.out.println(arrl);
      System.out.println("removeFirstOccurrence() method:" +arrl.removeFirstOccurrence("eight"));
      System.out.println("After removeFirstOccurrence() method call:");
      System.out.println(arrl);
      System.out.println("removeLast() method:"+arrl.removeLast());
      System.out.println("After removeLast() method call:");
      System.out.println(arrl);
      System.out.println("removeLastOccurrence() method:"+ arrl.removeLastOccurrence("five"));
      System.out.println("After removeLastOccurrence() method call:");
      System.out.println(arrl);
   }
}

Output:

[First, Second, Third, Random, four, five, six, seven, eight, nine]
Remov() method:First
After remove() method call: [Second, Third, Random, four, five, six, seven, eight, nine]
remove(index) method:Random
After remove(index) method call: [Second, Third, four, five, six, seven, eight, nine]
Remov(object) method:true
After remove(object) method call: [Second, Third, four, five, seven, eight, nine]
removeFirst() method:Second
After removeFirst() method call: [Third, four, five, seven, eight, nine]
removeFirstOccurrence() method:true
After removeFirstOccurrence() method call: [Third, four, five, seven, nine]
removeLast() method:nine
After removeLast() method call: [Third, four, five, seven]
removeLastOccurrence() method:true
After removeLastOccurrence() method call: [Third, four, seven]

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

How To Add Values Last LinkedList in Java With Example?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
public class Add_Values_Last_LinkedList {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      LinkedList<String> arrl = new LinkedList<String>();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.addLast("I am last");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.offerLast("I am last - 1");
        System.out.println(arrl);
        System.out.println("Adding element at last position...");
        arrl.offer("I am last - 2");
        System.out.println(arrl);
   }
}

Output:

[First, Second, Third, Random]
Adding element at last position...
[First, Second, Third, Random, I am last]
Adding element at last position...
[First, Second, Third, Random, I am last, I am last - 1]
Adding element at last position...
[First, Second, Third, Random, I am last, I am last - 1, I am last - 2]

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Different Basic Methods LinkedList In Java With Example?

package com.softwaretestingo.java.basics;
import java.util.LinkedList;
public class LinkedListOperations 
{
   public static void main(String[] args) 
   {
      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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

First LinkedList Value Using Java: Write a Program to Add Values First LinkedList In Java?

package com.java.Softwaretestingblog;
import java.util.LinkedList;
public class Add_Values_First_LinkedList {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      LinkedList<String> arrl = new LinkedList<String>();
      arrl.add("First");
      arrl.add("Second");
      arrl.add("Third");
      arrl.add("Random");
      System.out.println(arrl);
      System.out.println("Adding element at first position...");
      arrl.addFirst("I am first");
      System.out.println(arrl);
      System.out.println("Adding element at first position...");
      arrl.offerFirst("I am first - 2");
      System.out.println(arrl);
   }
}

Output:

[First, Second, Third, Random]
Adding element at first position...
[I am first, First, Second, Third, Random]
Adding element at first position...
[I am first - 2, I am first, First, Second, Third, Random]

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Another LinkedList Java Program: How to Copy Values Of One List To Another LinkedList Java with Example?

package com.java.Softwaretestingblog;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Copyvalues2LinkedlistinJava {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   LinkedList<String> arrl = new LinkedList<String>();
        //adding elements to the end
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Actual LinkedList:"+arrl);
        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        arrl.addAll(list);
        System.out.println("After Copy: "+arrl);
   }
}

Output:

Actual LinkedList:[First, Second, Third, Random]
After Copy: [First, Second, Third, Random, one, two]

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers