How to Print an ArrayList in Java

Rupam Yadav Feb 02, 2024
  1. Print Arraylist in Java Using the for Loop
  2. Print Arraylist in Java Using forEach
  3. Print Arraylist in Java Using IDs
  4. Print Arraylist in Java Using the toString() Command
How to Print an ArrayList in Java

In this article, we’ll go over multiple ways to print an ArrayList in Java. Our Arraylist will contain a class object called ModelClass, which has a variable name and getter/setter functions.

We can print Java ArrayList object’s items using a loop. Here, we use the for loop to go through every ModelClass object inside modeList and call the getName() function, which returns the name.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<ModelClass> modelList;

    ModelClass m1 = new ModelClass();
    ModelClass m2 = new ModelClass();
    ModelClass m3 = new ModelClass();

    m1.setName("Sam");
    m2.setName("Kevin");
    m3.setName("Gwen");

    modelList = new ArrayList<ModelClass>();
    modelList.add(m1);
    modelList.add(m2);
    modelList.add(m3);

    for (int i = 0; i < modelList.size(); i++) {
      System.out.println(modelList.get(i).getName());
    }
  }
}

class ModelClass {
  private String name;
  void setName(String name) {
    this.name = name;
  }

  String getName() {
    return name;
  }
}

Output:

Sam
Kevin
Gwen

In Java, every ArrayList has a forEach method, which is one of the simplest ways to loop through all the items just like the for loop. Like the previous example, we can get the names from ModelClass using the getName() method.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    ArrayList<ModelClass> modelList;

    ModelClass m1 = new ModelClass();
    ModelClass m2 = new ModelClass();
    ModelClass m3 = new ModelClass();

    m1.setName("Sam");
    m2.setName("Kevin");
    m3.setName("Gwen");

    modelList = new ArrayList<ModelClass>();
    modelList.add(m1);
    modelList.add(m2);
    modelList.add(m3);

    modelList.forEach(new Consumer<ModelClass>() {
      @Override
      public void accept(ModelClass modelClass) {
        System.out.println(modelClass.getName());
      }
    });
  }
}

class ModelClass {
  private String name;
  void setName(String name) {
    this.name = name;
  }

  String getName() {
    return name;
  }
}

Output:

Sam
Kevin
Gwen

Every ArrayList element is given a unique ID to identify it; we can get this if we print the ArrayList without using any method like toString(). It will print the raw ArrayList with the item’s IDs, which you can see in the example’s output:

import java.util.ArrayList;

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

        ArrayList<ModelClass> modelList;

        ModelClass m1 = new ModelClass();
        ModelClass m2 = new ModelClass();
        ModelClass m3 = new ModelClass();

        m1.setName("Sam");
        m2.setName("Kevin");
        m3.setName("Gwen");

        modelList = new ArrayList<ModelClass>();
        modelList.add(m1);
        modelList.add(m2);
        modelList.add(m3);

        System.out.println(modelList);

    }
}

class ModelClass{

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

}

Output:

[ModelClass@682a0b20, ModelClass@3d075dc0, ModelClass@214c265e]

The last method in this list is overriding a toString() method inside the ModelClass. This function will return the name when we call it using modeList. Take note that this process can only return string values, as implied in its name.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    ArrayList<ModelClass> modelList;

    ModelClass m1 = new ModelClass();
    ModelClass m2 = new ModelClass();
    ModelClass m3 = new ModelClass();

    m1.setName("Sam");
    m2.setName("Kevin");
    m3.setName("Gwen");

    modelList = new ArrayList<ModelClass>();
    modelList.add(m1);
    modelList.add(m2);
    modelList.add(m3);

    System.out.println(modelList.toString());
  }
}

class ModelClass {
  private String name;
  void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "ModelClass{"
        + "name='" + name + '\'' + '}';
  }
}

Output:

[ModelClass{name='Sam'}, ModelClass{name='Kevin'}, ModelClass{name='Gwen'}]
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java ArrayList