BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

When to use ArrayList and LinkedList in Java

By Chaitanya Singh | Filed Under: java

In this guide, we will discuss, when to use ArrayList and when to use LinkedList in Java. There are multiple similarities between these classes. However there is a significant difference between ArrayList and LinkedList, when we talk about the performance of various operations such as add, update, delete, search etc.

When to use ArrayList and when to use LinkedList in Java

1) LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. This is because every time you add an element, Java ensures that it can fit the element so it grows the ArrayList. Similar case with the remove operation.

The insert and remove operations give good performance (O(1)) in LinkedList compared to ArrayList(O(n)). Hence if there are frequent addition and deletion in application then LinkedList is a best choice.

2) ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

Search (get method) operations are fast in Arraylist (O(1)) but not in LinkedList (O(n)) so, if there are less add and remove operations and more search operations requirement, ArrayList would be your best bet.

ArrayList Example in Java

import java.util.*;
public class JavaExample {
  public static void main(String[] args) {
    ArrayList<String> list=new ArrayList<String>();
    list.add("Chaitanya");
    list.add("BeginnersBook");
    list.add("Website");
    list.add("Tutorials");

    System.out.println("ArrayList elements: ");
    for(String str:list){
      System.out.println(str);
    }
  }
}

Output:

ArrayList elements: 
Chaitanya
BeginnersBook
Website
Tutorials

LinkedList Example in Java

import java.util.*;
public class JavaExample {
  public static void main(String[] args) {
    LinkedList<String> fruits = new LinkedList<String>();
    fruits.add("Apple");
    fruits.add("Mango");
    fruits.add("Banana");
    fruits.add("Orange");

    //print LinkedList
    System.out.println("LinkedList elements: ");
    for(String str:fruits){
      System.out.println(str);
    }
  }
}

Output:

LinkedList elements: 
Apple
Mango
Banana
Orange

Recommended Articles:

  • How to join two ArrayLists
  • Compare two ArrayLists
  • How to serialize an ArrayList in Java
❮ Java Collections

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap