Data Structure & Algorithm in Java

  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Linked Lists
  5. Lists in java.util.

Lists in java.util.

  1. LinkedList:
    • java.util.LinkedList implements the List interface using a doubly-linked list.
    • Each element in the list is stored as a separate object (node) containing a reference to the previous and next elements in the list.
    • Insertion and deletion operations can be performed efficiently at any position in the list.
    • Traversal might be slower compared to arrays because elements are not stored in contiguous memory locations.
    • Suitable for scenarios where frequent insertion and deletion operations are required, especially near the beginning or middle of the list.
    • Not suitable for random access operations (i.e., accessing elements by index) due to slower traversal.
  2. ArrayList:
    • java.util.ArrayList implements the List interface using a dynamic array.
    • Elements are stored in a contiguous memory block, allowing for efficient random access using indexes.
    • Insertion and deletion operations at the end of the list are efficient. However, inserting or removing elements from the middle of the list requires shifting subsequent elements, which can be costly.
    • Suitable for scenarios where frequent random access and traversal operations are required.
    • Offers better performance for accessing elements by index compared to LinkedList.
    • Automatically grows its internal array when needed to accommodate more elements.
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // LinkedList Example
        List<String> linkedList = new LinkedList<>();

        for (String fruit : linkedList) {

        // ArrayList Example
        List<String> arrayList = new ArrayList<>();

        for (String fruit : arrayList) {

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *