How to print out all the elements of a List in Java?

print elements of arraylist java
java print arraylist of objects
print list in java without loop
print list in logger java
how to print arraylist in java using for loop
java 8 print list of objects
how to print list using for loop in java
print list of arrays java

I am trying to print out all the elements of a List, however it is printing the pointer of the Object rather than the value.

This is my printing code...

for(int i=0;i<list.size();i++){

Could anyone please help me why it isn't printing the value of the elements.

Here is some example about getting print out the list component:

public class ListExample {

    public static void main(String[] args) {
        List<Model> models = new ArrayList<>();

        // TODO: First create your model and add to models ArrayList, to prevent NullPointerException for trying this example

        // Print the name from the list....
        for(Model model : models) {

        // Or like this...
        for(int i = 0; i < models.size(); i++) {

class Model {

    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

How to print out all the elements of a List in Java?, In this post, we will discuss various ways to iterate over a List in Java. 1. Using List.toString() - If We can also use List.iterator() that returns an Iterator . So, we need to convert our list to Vector first and then print all elements of that Vector. Java Collection, ArrayList Exercises: Exercise-22 with Solution. Write a Java program to print all the elements of a ArrayList using the position of the elements.

The following is compact and avoids the loop in your example code (and gives you nice commas):


However, as others have pointed out, if you don't have sensible toString() methods implemented for the objects inside the list, you will get the object pointers (hash codes, in fact) you're observing. This is true whether they're in a list or not.

Java Program to Iterate over a List, forEach() can be used to print all elements of ArrayList in Java. Detailed Example Java using Java For Loop. Basic Java For Loop can be used to iterate through all the elements of an ArrayList. Java - Files. Java - List files and sub-folders. For Loop or ArrayList.forEach() can be used to print all elements of ArrayList in Java. Detailed Example Java programs have been provided.

Since Java 8, List inherits a default "forEach" method which you can combine with the method reference "System.out::println" like this:


How to Print all Elements of an ArrayList in Java?, To print elements, first we'll create a String ArrayList and store weekdays name. We have created an object of the list and added name of week days Complete Code to print ArrayList Elements in Java using all 4 Ways. In this tutorial, we show you how to print all elements of an ArrayList in Java. In this example below, we will display all the elements of a ArrayList using Loop . Print All Elements of an ArrayList using for (Object object : list)

System.out.println(list);//toString() is easy and good enough for debugging.

toString() of AbstractCollection will be clean and easy enough to do that. AbstractList is a subclass of AbstractCollection, so no need to for loop and no toArray() needed.

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

If you are using any custom object in your list, say Student , you need to override its toString() method(it is always good to override this method) to have a meaningful output

See the below example:

public class TestPrintElements {

    public static void main(String[] args) {

        //Element is String, Integer,or other primitive type
        List<String> sList = new ArrayList<String>();

        //Element is custom type
        Student st1=new Student(15,"Tom");
        Student st2=new Student(16,"Kate");
        List<Student> stList=new ArrayList<Student>();

public  class Student{
    private int age;
    private String name;

    public Student(int age, String name){

    public String toString(){
        return "student "+name+", age:" +age;


[string1, string2]
[student Tom age:15, student Kate age:16]

Here Is 4 Ways To Print ArrayList Elements In Java, Learn how to convert a List to a String using different techniques. The high level overview of all the articles on the site. CHECK OUT THE COURSE This might be useful in certain scenarios like printing the contents to the utilizes the toString() method of the type of the elements within the List. In our  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.

The Java 8 Streams approach...;

Converting a List to String in Java, Java Collection, ArrayList Exercises: Print all the elements of a array list: " + c1​); System.out.println("\nPrint using index of an element: "); int  Java 8 Stream API. As of Java 8, we can also use the Stream API to find an element in a List. To find an element matching specific criteria in a given list, we: invoke stream() on the list. call the filter() method with a proper Predicate.

Java Collection exercises: Print all the elements of a ArrayList using , Java – Print array elements. By Lokesh Recommended way to print the content of an array is using Arrays. A normal java list having four String objects. System.out.println(list) should print all the standard java object types (String, Long, Integer etc). In case, if we are using custom object types, then we need to override toString() method of our custom object.

Java – Print array elements, Print All Elements of an ArrayList using for (Object object : list). package com.​jackrutorial; import java.util.ArrayList; import java.util.List; public  For displaying all keys or values present in the map, we can simply print the string representation of keySet () and values () respectively. Below is a simple Java program that print all keys of a Map using keySet () in Java – Download Run Code. Similarly, below Java program print all values of a Map using values () in Java – Download Run Code.

How to Print all Elements of an Arraylist in Java, In order to print integer array, all you need to do is call Arrays.toString(int array) method and pass your integer array to it. This method will take care of printing  There are 3 ways to print the elements of a Stream in Java: forEach() println() with collect() peek() Below are the three ways to print the Stream in detail:

  • What type did you declare the List to be? Show us how you declared and instantiated it.
  • you have to call toString and youll get a explanation of the class or override the toString method for the type that the list contains
  • That's what you're telling it to print--you need a different toString or other readable string.
  • ArrayList<class> list= new ArrayList<class>();
  • Note that there's a more compact syntax you can use to accomplish the same thing: for (Object obj : list) {System.out.println(obj);}.
  • How is the introduction of the Model class related to the question?
  • It's just an assumption, since I don't know what kind of Object inside the list.
  • What about only list.toString().
  • Just using 'list.toString()' won't print the individual elements, unless it's a custom implementation of the List interface which overrides the normal behaviour (to print the class name and a hash code, more or less).
  • If they use custom class and does not override toString,your solution will also prints its class name and hash code.