Java LinkedList

The LinkedList class provides a linked-list data structure and implements the List interface. It can have duplicate elements. The LinkedList is based on containers. The elements are saved in containers. Each container links to the next container. It is a part of the java.util package i.e., you need to import the java.util.LinkedList package.

Let us see some LinkedList methods/  operations with examples:

  • Create a LinkedList
  • Size of a LinkedList
  • Loop through the LinkedList
  • Add an item to the beginning of the LinkedList
  • Add an item to the end of the LinkedList
  • Remove an item from the beginning of the LinkedList
  • Remove an item from the end of the LinkedList
  • Get the item at the beginning of the LinkedList
  • Get the item at the end of the LinkedList

Let us first see how to create a LinkedList in Java:

Create a LinkedList

To create a LinkedList, set the type in angular braces i.e., use the wrapper class. For example, for creating a LinkedList with string elements, use <String>:

LinkedList<String> myList = new LinkedList<String>();

Let us now see an example and create a LinkedList in Java with string elements:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println(sports);
  } 
}

Output

[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Size of a LinkedList

To get the size of a LinkedList i.e., how many elements it has, use the size() method. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println(sports);
    
    // Display the LinkedList size
    System.out.println("Count of elements = "+sports.size());
  } 
}

Output

[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]
Count of elements = 6

Loop through the LinkedList

Use the for loop to loop through the LinkedList in Java. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println(sports);
    
    // Loop through the LinkedList
    for (int i = 0; i < sports.size(); i++) {
      System.out.println("Element "+(i+1)+" = "+sports.get(i));
    }    
  } 
}

Output

[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]
Element 1 = Cricket
Element 2 = Football
Element 3 = Tennis
Element 4 = Badminton
Element 5 = Basketball
Element 6 = Volleyball

Add an item to the beginning of the LinkedList

The addFirst() method in Java adds an item to the beginning of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Add an item to the beginning of the LinkedList
    sports.addFirst("Golf");
    System.out.println("\nUpdated LinkedList\n"+sports);
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Updated LinkedList
[Golf, Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Add an item to the end of the LinkedList

The addLast() method in Java adds an item to the end of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Add an item to the end of the LinkedList
    sports.addLast("Golf");
    System.out.println("\nUpdated LinkedList\n"+sports);
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Updated LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball, Golf]

Remove an item from the beginning of the LinkedList

The removeFirst() method in Java is used to remove an item from the beginning of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Remove an item from the beginning of the LinkedList
    sports.removeFirst();
    System.out.println("\nUpdated LinkedList\n"+sports);
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Updated LinkedList
[Football, Tennis, Badminton, Basketball, Volleyball]

Remove an item from the end of the LinkedList

The removeLast() method in Java is used to remove an item from the end of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Remove an item from the end of the LinkedList
    sports.removeLast();
    System.out.println("\nUpdated LinkedList\n"+sports);
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

Updated LinkedList
[Cricket, Football, Tennis, Badminton, Basketball]

Get the item at the beginning of the LinkedList

The getFirst() method in Java is used to get an item at the beginning of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Get the item at the beginning of the LinkedList
    System.out.println("\nThe 1st element\n"+sports.getFirst());
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

The 1st element
Cricket

Get the item at the end of the LinkedList

The getLast() method in Java is used to get an item at the end of the LinkedList. Let us see an example:

import java.util.LinkedList;

class Studyopedia { 

  public static void main(String[] args) { 
  
    // Create a LinkedList with string elements
    LinkedList<String> sports = new LinkedList<String>();
    
    // Adding string elements
    sports.add("Cricket");
    sports.add("Football");
    sports.add("Tennis");
    sports.add("Badminton");
    sports.add("Basketball");
    sports.add("Volleyball");
    
    // Display the LinkedList
    System.out.println("LinkedList\n"+sports);
    
    // Get the item at the end of the LinkedList
    System.out.println("\nThe last element\n"+sports.getLast());
  } 
}

Output

LinkedList
[Cricket, Football, Tennis, Badminton, Basketball, Volleyball]

The last element
Volleyball

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Java Method Overloading
Java ArrayList
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment