In this Java Tutorial, you will learn how to print all elements of an ArrayList using an enhanced for loop, ArrayList.forEach(), an index-based loop, a method reference, and Java Streams. You will also learn how to print an ArrayList on one line, without brackets, and how to display a list of custom objects.
Ways to Print All Elements of an ArrayList in Java
Java provides several ways to print the elements stored in an ArrayList. The appropriate approach depends on whether you need each element on a separate line, access to the element index, custom formatting, filtering, or a compact one-line result.
- Use an enhanced
forloop for simple and readable iteration. - Use an index-based
forloop when the element position is required. - Use
forEach()or a method reference for concise iteration. - Use
String.join()or Streams when custom separators or transformations are needed. - Use
System.out.println(arrayList)for a quick representation that includes square brackets.
Process 1: Java For Loop can be used to iterate through all the elements of an ArrayList.
Process 2: Java provides forEach(); method for ArrayList. Each element can be accessed using the parameter provided inside the forEach() function.
Print ArrayList Elements with an Enhanced For Loop
1. Print All Elements of ArrayList – For Loop
In the following example, we will initialize an ArrayList with some elements and print them using for loop.
PrintElements.java
import java.util.ArrayList;
public class PrintElements {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
for(String name:names) {
System.out.println(name);
}
}
}
Output
Java
Kotlin
Android
The enhanced for loop reads each element from names in insertion order and stores it temporarily in the variable name. This approach is suitable when the index is not needed.
Print ArrayList Elements with forEach()
2. Print All Elements of ArrayList – ArrayList.forEach()
In the following example, we will initialize an ArrayList with some elements and print them using ArrayList.forEach() method.
PrintElements.java
import java.util.ArrayList;
public class PrintElements {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
names.forEach(name ->{
System.out.println(name);
});
}
}
Output
Java
Kotlin
Android
The forEach() method accepts a Consumer. In this example, the lambda expression receives one element at a time and prints it.
Print ArrayList Elements Using a Method Reference
When the lambda expression only passes each element to System.out.println(), it can be replaced with the method reference System.out::println.
import java.util.ArrayList;
public class PrintWithMethodReference {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
names.forEach(System.out::println);
}
}
Output
Java
Kotlin
Android
Print ArrayList Elements with Their Index
Use an index-based loop when the position of each ArrayList element must be printed. The valid index range starts at 0 and ends at size() - 1.
import java.util.ArrayList;
public class PrintWithIndex {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
for (int i = 0; i < names.size(); i++) {
System.out.println(i + ": " + names.get(i));
}
}
}
Output
0: Java
1: Kotlin
2: Android
Print an Entire ArrayList on One Line
Passing an ArrayList directly to System.out.println() calls its toString() method. The elements are displayed in insertion order, separated by commas and enclosed in square brackets.
import java.util.ArrayList;
public class PrintArrayList {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
System.out.println(names);
}
}
Output
[Java, Kotlin, Android]
This form is useful for debugging. For user-facing output, use a custom separator or formatting method instead.
Print an ArrayList Without Square Brackets
For an ArrayList<String>, use String.join() to combine all elements with a chosen delimiter. The following example prints the list without square brackets.
import java.util.ArrayList;
public class PrintWithoutBrackets {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
System.out.println(String.join(", ", names));
}
}
Output
Java, Kotlin, Android
The first argument to String.join() is the delimiter inserted between consecutive strings. It can be changed to a space, comma, pipe, or another separator.
Print ArrayList Elements Using Java Streams
A Stream is useful when elements must be filtered, mapped, or otherwise processed before they are printed.
import java.util.ArrayList;
public class PrintWithStream {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Java");
names.add("Kotlin");
names.add("Android");
names.stream()
.filter(name -> name.length() > 4)
.map(String::toUpperCase)
.forEach(System.out::println);
}
}
Output
KOTLIN
ANDROID
The stream filters out strings whose length is not greater than four, converts the remaining strings to uppercase, and prints them.
Print an ArrayList of Objects in Java
When an ArrayList contains custom objects, printing each object directly uses that object’s toString() method. Override toString() to define a readable representation.
import java.util.ArrayList;
class Student {
private final int id;
private final String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "'}";
}
}
public class PrintObjectList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(101, "Asha"));
students.add(new Student(102, "Ravi"));
students.forEach(System.out::println);
}
}
Output
Student{id=101, name='Asha'}
Student{id=102, name='Ravi'}
Without an overridden toString() method, the default object representation normally contains the class name and a hash-based value rather than the object’s field values.
Choose the Right ArrayList Printing Method
| Requirement | Recommended approach |
|---|---|
| Print each element on a new line | Enhanced for loop or forEach(System.out::println) |
| Print each element with its index | Index-based for loop |
| Print the complete list quickly | System.out.println(arrayList) |
| Print strings without brackets | String.join() |
| Filter or transform before printing | Java Stream |
| Print custom objects clearly | Override toString() or print selected fields |
Common Problems When Printing Java ArrayLists
- Printing the ArrayList variable includes brackets: Use
String.join()for strings or a Stream collector when custom formatting is required. - Custom objects display unreadable values: Override the object’s
toString()method or print individual fields. - An index-based loop throws an exception: Use the condition
i < list.size(), noti <= list.size(). - The list prints nothing: Check whether the ArrayList is empty before iteration.
- A null element prints as null: Test each value before calling methods on it if null entries are possible.
Java ArrayList Printing FAQs
How do you print all elements of an ArrayList in Java?
Use an enhanced for loop, forEach(), or forEach(System.out::println). Each approach visits the elements in the ArrayList’s iteration order.
How do you print an ArrayList without brackets in Java?
For a list of strings, use String.join(", ", list). This joins the elements with a delimiter without adding square brackets.
How do you print an ArrayList of objects in Java?
Iterate over the objects and print either their fields or the objects themselves. When printing the objects directly, override toString() in the object’s class to produce readable output.
How do you print an ArrayList as a single string?
Calling arrayList.toString() returns a bracketed representation. For a customized string, use String.join() for strings or Streams with Collectors.joining().
Should I use forEach or a for loop to print an ArrayList?
Use forEach() for concise element-by-element processing. Use an index-based for loop when you need positions, need to access nearby elements, or require more explicit control over iteration.
Java ArrayList Printing Editorial QA Checklist
- Verify that every ArrayList example imports
java.util.ArrayList. - Confirm that index-based loops use
i < list.size(). - Check that each output block matches the order of inserted ArrayList elements.
- Confirm that
String.join()is used only with compatible character-sequence elements. - Verify that custom object examples provide a readable
toString()implementation. - Check that Stream examples explain any filtering or transformation applied before printing.
Conclusion
In this Java Tutorial, we learned how to print elements of an ArrayList using an enhanced for loop, an index-based loop, ArrayList.forEach(), a method reference, and Java Streams. We also covered printing an ArrayList on one line, removing square brackets with String.join(), and displaying custom objects with an overridden toString() method.
TutorialKart.com