- How to Print ArrayList in Java
- 8 ways to Print ArrayList in Java
- 1. Print ArrayList using for loop
- 2. Print ArrayList using enhanced for each loop
- 3. Print ArrayList using the Iterator interface
- 4. Print ArrayList using the ListIterator interface
- 5. Print ArrayList using println() method
- 6. Print ArrayList using toString() method
- 7. Print ArrayList using the forEach() method with a lambda expression
- 8. Print ArrayList using Stream class (Java 8)
- Conclusion:
- Java print ArrayList example
- How to print ArrayList in Java?
- 1) Using for loop
- 2) Using enhanced for loop
- 3) Using Arrays class
- 4) Using Java 8 stream
- How to print ArrayList containing custom objects?
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.
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
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
Java print ArrayList example
Java print ArrayList example shows how to print ArrayList in Java. The example also shows various ways to print the ArrayList using a loop, Arrays class, and Java 8 Stream.
How to print ArrayList in Java?
There are several ways using which you can print ArrayList in Java as given below.
1) Using for loop
You can print ArrayList using for loop in Java just like an array.
2) Using enhanced for loop
You can also use the enhanced for loop instead of the for loop it iterate over the elements of an ArrayList and print.
3) Using Arrays class
You can convert ArrayList to an array and use the toString method of Arrays class to print the elements.
If you want to remove the square brackets, you can remove them using the replaceAll method as given below.
4) Using Java 8 stream
If you are using Java 8, you can use the stream as given below.
Or you can use below given statement to print the elements of ArrayList.
How to print ArrayList containing custom objects?
How to print ArrayList containing objects of a custom class? For example, consider below given code containing ArrayList of Person class objects.
Instead of printing the contents of the Person objects, our code printed memory locations of the Person object. The reason is since the Person class does not override the toString method, the default toString method of the Object class is called which prints the address location of the object.
Let’s override the toString method in the Person class to get the contents of the Person object instead of the memory locations.