20 Jul 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>:
1 2 3 |
LinkedList<String> myList = new LinkedList<String>(); |
Let us now see an example and create a LinkedList in Java with string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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
1 2 3 |
[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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
1 2 3 4 |
[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 5 6 7 8 9 |
[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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
1 2 3 4 5 6 7 |
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
No Comments