How to take all elements of ArrayList inwards Java - Example

There are 2 ways to withdraw all elements of an ArrayList inwards Java, either yesteryear using clear() or  by using the removeAll() method. Both methods are defined inwards the java.util.List in addition to java.util.Collection interface, so they are available non simply to ArrayList but besides to Vector or LinkedList etc. Both elements removes all objects from ArrayList but in that place is a subtle departure inwards how they do. The clear() method is straightforward, it traverse through the ArrayList in addition to sets all indices to null, which agency the ArrayList becomes empty in addition to all elements acquire eligible to Garbage collection, provided in that place is no to a greater extent than references to them. The fourth dimension taken yesteryear clear() method is inwards O(n), which agency the bigger the arraylist the longer it volition convey to empty it.

On the other mitt removeAll(Collection c) accepts a Collection in addition to and so iterate over List. At each iteration it checks if electrical current chemical component inwards the ArrayList is acquaint inwards the Collection c using contains() method, which takes its O(n) fourth dimension to confirm because it besides uses Iterator instead of random access.

So overall fourth dimension taken to withdraw all elements using removeAll() method is inwards companionship of O(n^2) which agency fourth dimension volition increase inwards quadratic of publish of elements inwards array. This departure is non much if your ArrayList is modest in addition to simply contains 10 to 100 elements but for a large ArrayList e.g. of 1 1 1000 k objects, this fourth dimension could move significant.

This is besides 1 of the often asked ArrayList question from Java Interviews, so knowing the cardinal departure volition aid y'all in that place equally well.  So alternative is yours, I advise to purpose clear() if y'all desire to withdraw all elements from the ArrayList in addition to purpose removeAll() if y'all desire to withdraw selected elements given to y'all inwards a Collection. Let's come across the event of both of them inwards Java.




How to empty an ArrayList inwards Java

Here is a consummate Java plan to withdraw all elements in addition to brand an ArrayList empty inwards Java. This plan demonstrate how y'all tin withdraw all elements from a given ArrayList yesteryear using both clear() in addition to removeAll() method. If y'all desire to withdraw simply unmarried chemical component in addition to so y'all tin purpose the remove() method equally discussed here.

The plan prints all objects of ArrayList earlier in addition to after calling the clear() in addition to removeAll() method to demo that method is genuinely working in addition to the ArrayList is empty afterwards.

You tin reuse the ArrayList yesteryear clearing it but brand certain y'all don't exercise that inwards multi-threading surround e.g. 1 thread is calling the clear() method land other thread is calling the add() method to insert elements.  The ArrayList degree is non thread-safe in addition to sharing the same ArrayList betwixt multiple thread volition crate thread-safety related occupation in addition to erroneous result.

See Core Java for the Impatient to larn to a greater extent than close problems of using ArrayList inwards muti-threading application.

 There are 2 ways to withdraw all elements of an ArrayList inwards Java How to withdraw all elements of ArrayList inwards Java - Example


Java Program to brand an ArrayList empty

import java.util.ArrayList;  /*  * Java Program to withdraw all elements of ArrayList.  * This is besides known equally emptying an AraryList  */  public class Main {    public static void main(String[] args) {     System.out.println("Welcome to Java Program to empty an ArrayList");      ArrayList<String> listOfInsurance = new ArrayList<>();      listOfInsurance.add("Car Insurnace");     listOfInsurance.add("Health Insurnace");     listOfInsurance.add("Life Insurance");     listOfInsurance.add("Home Furniture Insurance");     listOfInsurance.add("Home loan Insurance");      System.out.println("ArrayList earlier emptying: ");     System.out.println(listOfInsurance);      // Emptying an ArrayList inwards Java     listOfInsurance.clear();      System.out.println("ArrayList after emptying: ");     System.out.println(listOfInsurance);      ArrayList<String> listOfLoans = new ArrayList<>();      listOfLoans.add("Car loan");     listOfLoans.add("Persona loan");     listOfLoans.add("Balance transfer");     listOfLoans.add("Home loan");      System.out.println("ArrayList earlier removing all elements: ");     System.out.println(listOfLoans);      // Emptying an ArrayList inwards Java     listOfLoans.removeAll(listOfLoans);      System.out.println("ArrayList after removing all elements: ");     System.out.println(listOfLoans);   }  }  Output Welcome to Java Program to empty an ArrayList ArrayList earlier emptying:  [Car Insurnace, Health Insurnace, Life Insurance, Home Furniture Insurance, Home loan Insurance] ArrayList after emptying:  [] ArrayList earlier removing all elements:  [Car loan, Persona loan, Balance transfer, Home loan] ArrayList after removing all elements:  []

You tin come across that both the ArrayLists are empty after calling the clear() in addition to removeAll() methods. So its working!!

That's all close how to withdraw all elements from an ArrayList inwards Java. As I said, clear() takes less fourth dimension than removeAll() to withdraw all objects, so y'all should ever purpose clear() to make an ArrayList empty. But, if y'all are non removing all elements in addition to listing of elements to move removed are provided to y'all inwards a Collection or List in addition to so purpose the removeAll() method.

Other Java ArrayList tutorials for Java Programmers
  • How to loop over ArrayList inwards Java? (answer)
  • How to create in addition to initialize the ArrayList inwards 1 line? (answer)
  • How to form an ArrayList inwards Java? (answer)
  • How to convert an ArrayList to String inwards Java? (example)
  • How to withdraw duplicates from ArrayList inwards Java? (example)
  • How to opposite an ArrayList inwards Java? (solution)
  • How to acquire the kickoff in addition to final chemical component of ArrayList inwards Java? (solution)
  • How to declare ArrayList amongst values inwards Java? (example)
  • How to acquire a make of elements equally sublist from ArrayList inwards Java? (example)
  • How convert ArrayList to HashSet inwards Java? (example)

Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
What's New inwards Java 8

Subscribe to receive free email updates:

0 Response to "How to take all elements of ArrayList inwards Java - Example"

Posting Komentar