How to Print ArrayList in Java

In this article, we will explore different ways of printing an ArrayList in Java. Before that, we should know what is ArrayList and Why we need to use it in creating Java applications. You can refer to my previous blogs in Javahungry to know about ArrayList in Java.

Read Also: Difference between ArrayList and Array in Java

Now, we will start exploring the different ways of printing ArrayList in Java with code examples.
1. Using for loop.
2. Using enhanced for each loop.
3. Using the Iterator interface.
4. Using the ListIterator interface.
5. Using println() method.
6. Using toString() method.
7. Using the forEach method with a lambda expression.
8. Using Stream class
    a. forEach method with a double colon lambda expression.
    b. forEach println() method.
    c. peek() and collect() methods.

8 ways to Print ArrayList in Java


Note: All the below 8 ways shall produce the same Output i.e
Employee Names are as follows,
Metilda
Sundhar
Ananya
Riya
Joy


1. Print ArrayList using for loop


In the below code snippet, we have used for loop to iterate and print the String ArrayList objects.
 import java.util.ArrayList;

public class TestArrayList1 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /* Printing String ArrayList object using for loop */
    System.out.println("Employee Names are as follows," + " ");
    for (int i = 0; i < employeeNames.size(); i++) {
      System.out.println(employeeNames.get(i) + " ");
    }
  }
}


2. Print ArrayList using enhanced for each loop


In the code snippet, we have used enhanced for each loop to iterate and print the String ArrayList objects.

 import java.util.ArrayList;

public class TestArrayList2 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /* Printing String ArrayList object using for each */
    System.out.println("Employee Names are as follows," + " ");
    for (String employeeName: employeeNames) {
      System.out.println(employeeName);
    }
  }
}


3. Print ArrayList using the Iterator interface


Here in the below code snippet, we have used the iterator ()method from the Iterator interface. This method returns an iterator for the String ArrayList object. The Iterator interface has methods hasNext() to check if the next element is available and return the element using the next() method. Using the Iterator interface, we can traverse only in a forward direction in the list object.

 import java.util.ArrayList;
import java.util.Iterator;
public class TestArrayList3 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /*Declare and initialize String Iterator object using iterator() method */
    Iterator < String > iterator = employeeNames.iterator();

    /* Printing String ArrayList object using Iterator */
    System.out.println("Employee Names are as follows," + " ");
    while (iterator.hasNext()) {
      System.out.println(iterator.next() + " ");
    }
  }
}


4. Print ArrayList using the ListIterator interface


Here in the below code snippet, we have used listIterator() method from the ListIterator interface. This method returns a list iterator for the String ArrayList object. The Iterator interface has methods hasNext() to check if the next element is available and return the element using the next() method. Using the ListIterator interface, we can traverse in both forward and backward directions in the list object.
  import java.util.ArrayList;
 import java.util.ListIterator;

 public class TestArrayList4 {
   public static void main(String args[]) {

     /* ArrayList Declaration */
     ArrayList < String > employeeNames = new ArrayList < String > ();

     /*Adding Strings to the String ArrayList object*/
     employeeNames.add("Metilda");
     employeeNames.add("Sundhar");
     employeeNames.add("Ananya");
     employeeNames.add("Riya");
     employeeNames.add("Joy");

     /*Declare and initialize String ListIterator object using listIterator() method */
     ListIterator < String > listIterator = employeeNames.listIterator();

     /* Printing String ArrayList object using ListIterator */
     System.out.println("Employee Names are as follows," + " ");
     while (listIterator.hasNext()) {
       System.out.println(listIterator.next() + " ");
     }
   }
 }


5. Print ArrayList using println() method


In the below code snippet, println() method is used to print the ArrayList object directly. ArrayList objects containing primitive data types can be printed directly using println() method.

 import java.util.ArrayList;

public class TestArrayList5 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /* Printing String ArrayList object */
    System.out.println("Employee Names are" + " " + employeeNames);
  }
}


6. Print ArrayList using toString() method


Here in the below code snippet, we are overriding toString() method to print the custom class ArrayList object. If the toString() method is not overridden ,the compiler prints the memory location of the objects.

 import java.util.ArrayList;
public class TestArrayList6 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < Object > employeeNames = new ArrayList < > ();

    /* Adding heterogeneous objects i.e String and Employee objects to ArrayList */
    employeeNames.add("Joy");
    employeeNames.add("Rose");
    employeeNames.add(new Employee("Anil"));
    employeeNames.add(new Employee("Mani"));

    /* Printing String and Employee ArrayList object */
    System.out.println("Employee names are" + " " + employeeNames);
  }
}
class Employee {

  String name;

  public Employee(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return getName();
  }

}

If the toString() method is not overridden, the compiler prints the memory location of the objects which will be as,
Output:
Employee names are[Joy, Rose, Employee@2f92e0f4, Employee@28a418fc]
We need to override the toString() method which tells the compiler how to print the custom class objects(here it is employee class). We get the below output after we override the toString() method.
Output:
Employee names are[Joy, Rose, Anil, Mani]

7. Print ArrayList using the forEach() method with a lambda expression


Java 8 gives a new method called forEach method to iterate the elements in the collection objects and Lambda expression to make our code readable and short.
In the below code snippet, we have used the forEach() method with a lambda expression to print the String ArrayList object.

 import java.util.ArrayList;

public class TestArrayList7 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /* Printing String ArrayList object using forEach method with lambda expression*/
    System.out.println("Employee Names are as follows" + " ");
    employeeNames.forEach(employeeName -> System.out.println(employeeName + " "));
  }
}


8. Print ArrayList using Stream class (Java 8)


a. forEach method with double colon lambda expression.
b. forEach println() method.
c. peek() and collect() methods.
Java 8 introduces Stream class which provides forEach(), peek()and collect() methods to print the ArrayList object as given in the below code snippet.

 import java.util.ArrayList;
import java.util.stream.Collectors;

public class TestArrayList8 {
  public static void main(String args[]) {

    /* ArrayList Declaration */
    ArrayList < String > employeeNames = new ArrayList < String > ();

    /*Adding Strings to the String ArrayList object*/
    employeeNames.add("Metilda");
    employeeNames.add("Sundhar");
    employeeNames.add("Ananya");
    employeeNames.add("Riya");
    employeeNames.add("Joy");

    /* Print String ArrayList object using forEach double colon lambda expression with Stream class object*/
    System.out.println("Employee Names are as follows");
    employeeNames.stream().forEach(System.out::println);
    /* Print String ArrayList object forEach println()method */
    System.out.println("Employee Names are as follows");
    employeeNames.stream().forEach(employeeName -> System.out.println(employeeName));
    /* Print String ArrayList object using Stream class collect() method */
    System.out.println("Employeee names are" + " " + employeeNames.stream().collect(Collectors.toList()));

    /*Print String ArrayList object using peek() and collect() methods*/
    employeeNames.stream().peek(System.out::println).collect(Collectors.toList());
  }
}


Conclusion:


After trying all the different ways of printing ArrayList in Java, we recommend to use Java 8 new features to make the code readable, reduce lines of code and improve the performance in accessing collections. We used println(), Stream class methods for debugging the code. In the case of the custom class, we used the iterator() and toString() method to print the ArrayList object.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry