Collections
Java Program to Demonstrate LinkedList
A Java program to demonstrate the usage of LinkedList.
Problem Description
Write a Java program to demonstrate basic operations on a LinkedList.
Code
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> animals = new LinkedList<>();
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Cow");
System.out.println("LinkedList: " + animals);
// Add element at specific index
animals.add(1, "Horse");
System.out.println("Updated LinkedList: " + animals);
}
}Output
LinkedList: [Dog, Cat, Cow]
Updated LinkedList: [Dog, Horse, Cat, Cow]Explanation
- LinkedList: Doubly-linked list implementation of the List and Deque interfaces.
- Performance: Better for frequent insertions and deletions compared to ArrayList.
