How to Print List in Java

  1. Print List in Java Using the Enhanced for Loop
  2. Print List in Java Using toString()
  3. Print List in Java Using forEach()

We will go through a few methods that can print out all the list items in Java. In the examples, we will use a model class to demonstrate how we can create a list of model objects and then print items in them.

To get all the elements from our Java list, we will create an enhanced loop that will loop through every item in the list and then print it out. In our case, a list item is a class object. Thus we have to call the method of every object to print list items.

Example:

import java.util.ArrayList;
import java.util.List;

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

        List<DummyModel> dummyModels = new ArrayList<>();

        DummyModel dm1 = new DummyModel();
        dm1.setName("John Doe");
        DummyModel dm2 = new DummyModel();
        dm2.setName("Sam Alex");
        DummyModel dm3 = new DummyModel();
        dm3.setName("Max Payne");
        DummyModel dm4 = new DummyModel();
        dm4.setName("Jp Cooper");
        dummyModels.add(dm1);
        dummyModels.add(dm2);
        dummyModels.add(dm3);
        dummyModels.add(dm4);

        for (DummyModel model : dummyModels) {
            System.out.println(model.getName());
        }

    }
}

class DummyModel {

    private String name;

    public String getName() {
        return name;
    }

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

Output:

John Doe
Sam Alex
Max Payne
Jp Cooper

We insert only String values into the model class, and to get the values back from the list, we can override the toString() and return the item through it. We will get an array of items.

Example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

        List<DummyModel> dummyModels = new ArrayList<>();

        DummyModel dm1 = new DummyModel();
        dm1.setName("John Doe");
        DummyModel dm2 = new DummyModel();
        dm2.setName("Sam Alex");
        DummyModel dm3 = new DummyModel();
        dm3.setName("Max Payne");
        DummyModel dm4 = new DummyModel();
        dm4.setName("Jp Cooper");
        dummyModels.add(dm1);
        dummyModels.add(dm2);
        dummyModels.add(dm3);
        dummyModels.add(dm4);
        
        System.out.println(dummyModels.toString());

    }
}

class DummyModel {

    private String name;

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

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

Output:

[John Doe, Sam Alex, Max Payne, Jp Cooper]

The last way to print a list in Java is to use the forEach() method introduced in Java 8. Every ArrayList has a forEach() method that processes every individual item from the List. We will use it to print out every item.

Example:

import java.util.ArrayList;
import java.util.List;

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

        List<DummyModel> dummyModels = new ArrayList<>();

        DummyModel dm1 = new DummyModel();
        dm1.setName("John Doe");
        DummyModel dm2 = new DummyModel();
        dm2.setName("Sam Alex");
        DummyModel dm3 = new DummyModel();
        dm3.setName("Max Payne");
        DummyModel dm4 = new DummyModel();
        dm4.setName("Jp Cooper");
        dummyModels.add(dm1);
        dummyModels.add(dm2);
        dummyModels.add(dm3);
        dummyModels.add(dm4);

        dummyModels.forEach(System.out::println);

    }
}

class DummyModel {

    private String name;

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

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

Output:

John Doe
Sam Alex
Max Payne
Jp Cooper

Related Article - Java List

  • How to Convert an Array to a List in Java
  • How to Join Two Lists in Java