Print List in Java
-
Print List in Java Using the Enhanced
for
Loop -
Print List in Java Using
toString()
-
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.
Print List in Java Using the Enhanced for
Loop
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
Print List in Java Using toString()
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]
Print List in Java Using forEach()
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
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java List
- Split a List Into Chunks in Java
- Get First Element From List in Java
- Find the Index of an Element in a List Using Java
- Filter List in Java
- Differences Between List and Arraylist in Java
- List vs. Array in Java