Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
ArrayList and LinkedList are two commonly used classes in Java for implementing dynamic lists. Both classes are part of the Java Collections Framework and implement the List interface. This tutorial will cover the basics of ArrayList and LinkedList, including their differences, use cases, and example code.
ArrayList is an implementation of the List interface backed by a dynamic array. It provides fast random access, but slower insertions and deletions compared to LinkedList, especially in the middle of the list.
import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> arrayList = new ArrayList<>(); // Add elements arrayList.add("Apple"); arrayList.add("Banana"); arrayList.add("Cherry"); // Access elements System.out.println(arrayList.get(1)); // Output: Banana // Remove elements arrayList.remove(0); // Iterate through the ArrayList for (String fruit : arrayList) { System.out.println(fruit); } } }
LinkedList is an implementation of the List interface backed by a doubly-linked list. It provides fast insertions and deletions, but slower random access compared to ArrayList. LinkedList is suitable for cases where you frequently need to add or remove elements at the beginning or end of the list.
import java.util.LinkedList; import java.util.List; public class LinkedListDemo { public static void main(String[] args) { List<String> linkedList = new LinkedList<>(); // Add elements linkedList.add("Apple"); linkedList.add("Banana"); linkedList.add("Cherry"); // Access elements System.out.println(linkedList.get(1)); // Output: Banana // Remove elements linkedList.remove(0); // Iterate through the LinkedList for (String fruit : linkedList) { System.out.println(fruit); } } }
Use ArrayList when:
Use LinkedList when:
This tutorial covered the basics of ArrayList and LinkedList in Java, their differences, use cases, and example code. Choosing between ArrayList and LinkedList depends on the specific requirements of your program and the operations you'll be performing most frequently.
Creating and initializing ArrayList in Java
// Creating and initializing ArrayList ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("Apple"); arrayList.add("Banana"); arrayList.add("Orange");
Adding and removing elements in ArrayList and LinkedList
// Adding elements arrayList.add("Grapes"); // Removing elements arrayList.remove("Banana");
Iterating through ArrayList and LinkedList in Java
// Iterating through ArrayList for (String fruit : arrayList) { System.out.println(fruit); }
Searching for elements in ArrayList and LinkedList
// Searching for elements if (arrayList.contains("Apple")) { System.out.println("Apple found!"); }
Memory usage comparison of ArrayList and LinkedList
ArrayList uses less memory per element than LinkedList due to its dynamic array implementation.
Sorting ArrayList and LinkedList in Java
// Sorting ArrayList Collections.sort(arrayList); // Sorting LinkedList Collections.sort(linkedList);
Converting ArrayList to LinkedList and vice versa
// Converting ArrayList to LinkedList LinkedList<String> linkedList = new LinkedList<>(arrayList); // Converting LinkedList to ArrayList ArrayList<String> arrayListConverted = new ArrayList<>(linkedList);