Java

How to Print a List in Java

List is a Java class that belongs to java.util package. It is inherited from the Collection interface and used to keep a collection in order. The List interface supports ArrayList and LinkedList. In Java programming, the ArrayList and LinkedList are frequently used, and the Java collection ArrayList implements the List.

This write-up will specifically discuss the method of printing a list in Java.

How to Print a List in Java?

To print a List in Java, you can use:

  • toString() method
  • for loop
  • for-each loop

Let’s check out each of the mentioned methods one by one!

Method 1: Print a List in Java Using toString() Method

The β€œtoString()” method can be used to print a list in Java. This method prints the specified list elements directly using their references.

Example
First, we will create a list of String type named β€œgadgetList” using the Java collection ArrayList:

List<String> gadgetList = new ArrayList<String>();

Then, we will add the values to the list with the help of the β€œadd()” method:

gadgetList.add("Mobile");
gadgetList.add("Computer");
gadgetList.add("Laptop");
gadgetList.add("Headphones");

Now, we will call the β€œtoString()” method to print the list on the console with the help of β€œSystem.out.println()” method:

System.out.println(gadgetList.toString());

Output

Want to perform the same operations using loops? Head towards the next sections!

Method 2: Print a List in Java Using for Loop

The most common method to print the List in Java is using a β€œfor” loop. The for loop iterates over the list until its size and prints out the values using the β€œSystem.out.println()” method.

Example
In this example we consider the same list named β€œgadgetList” and print its values using β€œfor” loop:

for(int i=0; i < gadgetList.size(); i++){
    System.out.println( gadgetList.get(i) );
}

The given output indicates that we have successfully displayed the list values with the help of the β€œfor” loop:

Method 3: Print a List in Java Using for-each Loop

The β€œfor-each” loop can also be utilized for the same purpose. It contains a variable with the same type as the List, colon, and the list’s name. This loop saves elements in the loop variable later printed on the console.

Example
In this example, we print the list named β€œgadgetList” using a β€œfor-each” loop. So, we will create a String type variable named β€œgadgets”. Then, print the elements of the list using the β€œSystem.out.println()” methods:

for(String gadgets : gadgetList){
      System.out.println(gadgets);
}

Output

We have offered all of the essential methods related to printing a list in Java.

Conclusion

To print a list in java, you can use three approaches: for loop, for-each loop, and toString() method. The for loop iterates over the list until its size and prints out the values using the System.out.println() method. The for-each loop contains a variable the same type as the List, colon, and the list’s name and saves elements in the loop variable. Lastly, the toString() method directly prints the specified list elements using their references. This write-up discussed the methods for printing a list in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.