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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaPrintArrayListExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListDays = new ArrayList<String>(); //add elements aListDays.add("Sunday"); aListDays.add("Monday"); aListDays.add("Tuesday"); System.out.println("Print Arraylist using for loop"); for(int i=0; i < aListDays.size(); i++){ System.out.println( aListDays.get(i) ); } } } |
Output
1 2 3 4 |
Print Arraylist using for loop Sunday Monday Tuesday |
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.
1 2 3 4 |
System.out.println("Print Arraylist using for each loop"); for( String strDay : aListDays ){ System.out.println(strDay); } |
Output
1 2 3 4 |
Print Arraylist using for each loop Sunday Monday Tuesday |
3) Using Arrays class
You can convert ArrayList to an array and use the toString
method of Arrays class to print the elements.
1 2 |
System.out.println("Print Arraylist using Arrays.toString method"); System.out.println( Arrays.toString(aListDays.toArray()) ); |
Output
1 2 |
Print Arraylist using Arrays.toString method [Sunday, Monday, Tuesday] |
If you want to remove the square brackets, you can remove them using the replaceAll
method as given below.
1 2 3 |
System.out.println( Arrays.toString(aListDays.toArray()) .replaceAll("[\\[\\]]", "") ); |
Output
1 |
Sunday, Monday, Tuesday |
4) Using Java 8 stream
If you are using Java 8, you can use the stream as given below.
1 2 |
//print list using Java 8 stream aListDays.stream().forEach(System.out::println); |
Or you can use below given statement to print the elements of ArrayList.
1 2 3 |
System.out.println( aListDays.stream().map(Object::toString).collect(Collectors.joining("\n")) ); |
Output
1 2 3 |
Sunday Monday Tuesday |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaPrintArrayListExample { public static void main(String[] args) { ArrayList<Person> aListPerson = new ArrayList<Person>(); aListPerson.add( new Person("Chuck", "Norris") ); aListPerson.add( new Person("Bruce", "Lee") ); aListPerson.add( new Person("Steven", "Seagal") ); for(Person p : aListPerson) System.out.println(p); } } class Person{ private String firstName; private String lastName; public Person(){} public Person(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } |
Output
1 2 3 |
com.javacodeexamples.collections.arraylist.Person@6d06d69c com.javacodeexamples.collections.arraylist.Person@7852e922 com.javacodeexamples.collections.arraylist.Person@4e25154f |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaPrintArrayListExample { public static void main(String[] args) { ArrayList<Person> aListPerson = new ArrayList<Person>(); aListPerson.add( new Person("Chuck", "Norris") ); aListPerson.add( new Person("Bruce", "Lee") ); aListPerson.add( new Person("Steven", "Seagal") ); for(Person p : aListPerson) System.out.println(p); } } class Person{ private String firstName; private String lastName; public Person(){} public Person(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String toString(){ return "[" + firstName + "," + lastName + "]"; } } |
Output
1 2 3 |
[Chuck,Norris] [Bruce,Lee] [Steven,Seagal] |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.
best answer thank you
Very useful code snippet. Thank you.
Glad you liked it, Thanks Jay.
Thank q u RahimV it was help to me I was struck on print of the list for my product class but it was printing class object address when i see your class example i had a changed according to your code Now it is working fine 🙂
Hello Syamsuresh,
Glad it helped.