Java – How to get size or length of an ArrayList

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

1. 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

2. 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

Related Articles:


Happy Coding !!
Happy Learning !!

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