Saturday, November 23, 2019

How To Take Away All Elements Of Arraylist Inwards Coffee - Example

There are 2 ways to take 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 as well as java.util.Collection interface, thence they are available non only to ArrayList but too to Vector or LinkedList etc. Both elements removes all objects from ArrayList but at that topographic point is a subtle divergence inwards how they do. The clear() method is straightforward, it traverse through the ArrayList as well as sets all indices to null, which agency the ArrayList becomes empty as well as all elements die eligible to Garbage collection, provided at that topographic point 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 paw removeAll(Collection c) accepts a Collection as well as and then iterate over List. At each iteration it checks if electrical flow chemical share inwards the ArrayList is introduce inwards the Collection c using contains() method, which takes its O(n) fourth dimension to confirm because it too uses Iterator instead of random access.

So overall fourth dimension taken to take all elements using removeAll() method is inwards company of O(n^2) which agency fourth dimension volition growth inwards quadratic of publish of elements inwards array. This divergence is non much if your ArrayList is modest as well as only contains 10 to 100 elements but for a large ArrayList e.g. of 1 1000000 objects, this fourth dimension could hold upwardly significant.

This is too i of the ofttimes asked ArrayList question from Java Interviews, so knowing the key divergence volition aid you lot at that topographic point equally well.  So pick is yours, I advise to role clear() if you lot desire to take all elements from the ArrayList as well as role removeAll() if you lot desire to take selected elements given to you lot inwards a Collection. Let's meet the instance of both of them inwards Java.




How to empty an ArrayList inwards Java

Here is a consummate Java plan to take all elements as well as brand an ArrayList empty inwards Java. This plan demonstrate how you lot tin take all elements from a given ArrayList yesteryear using both clear() as well as removeAll() method. If you lot desire to take only unmarried chemical share as well as then you lot tin role the remove() method equally discussed here.

The plan prints all objects of ArrayList earlier as well as after calling the clear() as well as removeAll() method to present that method is truly working as well as the ArrayList is empty afterwards.

You tin reuse the ArrayList yesteryear clearing it but brand certain you lot don't practise that inwards multi-threading environs e.g. i thread is calling the clear() method acre other thread is calling the add() method to insert elements.  The ArrayList class is non thread-safe as well as sharing the same ArrayList betwixt multiple thread volition crate thread-safety related occupation as well as erroneous result.

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

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


Java Program to brand an ArrayList empty

import java.util.ArrayList;  /*  * Java Program to take all elements of ArrayList.  * This is too 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 meet that both the ArrayLists are empty after calling the clear() as well as removeAll() methods. So its working!!

That's all nearly how to take all elements from an ArrayList inwards Java. As I said, clear() takes less fourth dimension than removeAll() to take all objects, thence you lot should ever role clear() to make an ArrayList empty. But, if you lot are non removing all elements as well as listing of elements to hold upwardly removed are provided to you lot inwards a Collection or List as well as then role the removeAll() method.

Other Java ArrayList tutorials for Java Programmers
  • How to loop over ArrayList inwards Java? (answer)
  • How to create as well as initialize the ArrayList inwards i line? (answer)
  • How to form an ArrayList inwards Java? (answer)
  • How to convert an ArrayList to String inwards Java? (example)
  • How to take duplicates from ArrayList inwards Java? (example)
  • How to contrary an ArrayList inwards Java? (solution)
  • How to acquire the starting fourth dimension as well as concluding chemical share of ArrayList inwards Java? (solution)
  • How to declare ArrayList alongside values inwards Java? (example)
  • How to acquire a attain of elements equally sublist from ArrayList inwards Java? (example)
  • How convert ArrayList to HashSet inwards Java? (example)

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java


No comments:

Post a Comment