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

How to empty an ArrayList in Java

By Chaitanya Singh | Filed Under: java

There are two ways to empty an ArrayList – By using ArrayList.clear() method or with the help of ArrayList.removeAll() method. Although both methods do the same task the way they empty the List is quite different.

Lets see the below example first then we will see the implementation and difference  between clear() and removeAll().

package beginnersbook.com;
import java.util.ArrayList;
import java.io.*;
public class Details
{
    public static void main(String [] args)
    {
       ArrayList<String> al1=new ArrayList<String>();
       al1.add("abc");
       al1.add("xyz");

       System.out.println("ArrayList before clear: "+al1);
       al1.clear();
       System.out.println("ArrayList after clear: "+al1);

       ArrayList<String> al2=new ArrayList<String>();
       al2.add("text 1");
       al2.add("text 2");

       System.out.println("ArrayList before removeAll: "+al2);
       al2.removeAll(al2);
       System.out.println("ArrayList before removeAll: "+al2); 
    }
}

Output:

ArrayList before clear: [abc, xyz]
ArrayList after clear: []
ArrayList before removeAll: [text 1="text" 2="2" language="1,"][/text]
ArrayList before removeAll: []

As you can both the methods did the same job, they emptied the ArrayList. It’s time to determine which method gives good performance.

The actual code of clear() method:

public void clear() {
    for (int i = 0; i < size; i++)
        arraylist[i] = null;

    size = 0;
}

Here arraylist is an instance of ArrayList class.

Code of removeAll() method:

public boolean removeAll(Collection c) {
    boolean ismodified = false;
    Iterator iterator = iterator();
    while (iterator.hasNext()) {
        if (c.contains(iterator.next())) {
            iterator.remove();
            ismodified = true;
        }
    }
    return ismodified;
}

By seeing the code of both the methods we can very well say that clear() method gives better performance compared to the removeAll() method.
Performance of clear: O(n)
Performance of removeAll: O(n^2)

Comments

  1. Koray says

    January 22, 2017 at 8:41 AM

    I think there is a typo at 3th line of first output example;

    “ArrayList before removeAll: [text 1, text 2]”

    Reply
  2. Rob says

    September 7, 2018 at 2:45 PM

    I don’t understand how the performance of removeAll is O(n^2), it looks like the while loop would just visit every element of the array once, the same way the for loop in clear would. O(n^2) usually means nested loops, but I don’t see that in the implementation of removeAll.

    Reply

Leave a Reply Cancel reply

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

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