The Java LinkedList class is a part of the Java Collections Framework and implements the List, Queue, and Deque interfaces. It is used for storing elements in a linked list structure, allowing for efficient insertion and removal operations.
LinkedList allows null elements, maintains insertion order, and is not synchronized. It provides constant-time performance for add and remove operations.
Let's create a LinkedList of strings and add some elements to it.
import java.util.LinkedList;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
}
}
Console Output:
[Apple, Banana, Cherry]
Access elements using the get()
method.
import java.util.LinkedList;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println("First element: " + list.get(0));
}
}
Console Output:
First element: Apple
Remove elements using the remove()
method.
import java.util.LinkedList;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.remove("Banana");
System.out.println(list);
}
}
Console Output:
[Apple, Cherry]
Use an iterator to traverse the elements.
import java.util.LinkedList;
import java.util.Iterator;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Console Output:
Apple
Banana
Cherry
Implement queue operations using LinkedList.
import java.util.LinkedList;
import java.util.Queue;
class Example {
public static void main(String args[]) {
Queue queue = new LinkedList();
queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");
System.out.println("Queue: " + queue);
System.out.println("Removed: " + queue.remove());
System.out.println("Queue after removal: " + queue);
}
}
Console Output:
Queue: [Apple, Banana, Cherry]
Removed: Apple
Queue after removal: [Banana, Cherry]
Perform deque operations like adding and removing elements from both ends.
import java.util.LinkedList;
import java.util.Deque;
class Example {
public static void main(String args[]) {
Deque deque = new LinkedList();
deque.addFirst("Apple");
deque.addLast("Banana");
deque.addFirst("Cherry");
System.out.println("Deque: " + deque);
deque.removeFirst();
System.out.println("Deque after removal: " + deque);
}
}
Console Output:
Deque: [Cherry, Apple, Banana]
Deque after removal: [Apple, Banana]
Use the size()
method to determine the number of elements in the LinkedList.
import java.util.LinkedList;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println("Size of the LinkedList: " + list.size());
}
}
Console Output:
Size of the LinkedList: 3
Use the clear()
method to remove all elements from the LinkedList.
import java.util.LinkedList;
class Example {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.clear();
System.out.println("LinkedList after clear operation: " + list);
}
}
Console Output:
LinkedList after clear operation: []
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies