How to get size or length of an ArrayList

In this article, we will discuss how to find size or length of an ArrayList


ArrayList :

We can use, size() method of Collection interface to find size of an ArrayList which

  • allows duplicate elements
  • null elements insertion is possible
  • maintains insertion-order


Example: To get size of an ArrayList using size() method


import java.util.ArrayList;
import java.util.Iterator;

public class FindSizeOfAnArrayList {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> al = new ArrayList<String>();

		// adding elements to ArrayList object
		al.add("Sundar Pichai");
		al.add("Satya Nadella");  
		al.add("Shiv Nadar");  
		al.add("Shantanu Narayen");
		al.add("Sundar Pichai"); // duplicate object
		al.add("Francisco D’Souza");
		al.add(null); // null is allowed

		int alSize = al.size();

		System.out.println("Size of an ArrayList is : " + alSize);

		// Iterating using Iterator of Collection interface
		System.out.println("\n\nIterating ArrayList elements\n");
		Iterator<String> itr = al.iterator();
		while(itr.hasNext()) {


Size of an ArrayList is : 7

Iterating ArrayList elements

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza

From above example, ArrayList

  • allows duplicate elements
  • null object is allowed
  • while iterating insertion-order is maintained




Happy Coding !!
Happy Learning !!

Adding element to ArrayList at specified index position
Iterating List using forEach() in Java 8