You are here
Home > java >

7 ways to print elements of a Collection in Java

7 ways to print elements of a Collection in JavaIn this article, we are going to learn 7 ways to print elements of a Collection in Java. However, I am sure that any java programmer will come across this situation multiple times while writing programs to develop any application/software. Especially while testing the program, printing of elements on console becomes a bit mandatory. Sometimes, it becomes confusing that which way we should choose to print the values of elements of the defined collection as per our requirement. I hope you will be able to tackle all the scenarios after going through this article accordingly. However, we will go step by step to get a hold on this topic.

Let’s consider a List as a Collection to demonstrate all the ways to print the elements. For example, let’s create a list of countries as below:

// Creating a List traditionally using JDK-8 or lower versions
List<String> countriesList =
  Arrays.asList(
    "USA","China","UK","Canada","Russia","India","Australia"
);

                           OR 

// Creating a List using List.of() method introduced in JDK-9
List<String> countriesList = 
 List.of(
  "USA","China","UK","Canada","Russia","India","Australia"
);

Using traditional for loop 

We will apply traditional for loop to print the elements of the list countriesList. For example, observe the below code.

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

Using for-each loop

The Java for-each loop or enhanced for loop is introduced in J2SE 5.0. It provides an alternative approach to traverse the array or collection in Java. Let’s see below how we can use this method to print elements of our countriesList. For example, observe the below code.

for(String country : countriesList) {
    System.out.println(country);
}

Using Iterator 

Iterator is an interface that belongs to a collection framework. However, It is also considered as a Universal iterator as you can apply it to any Collection object. Furthermore, It allows you to traverse the collection, accesses the data element and removes the data elements of the collection. Now, let’s see how we will use this Iterator to print the value of each element in our countriesList.

Iterator<String> itr= countriesList.iterator();
while (itr.hasNext()) {
   String country = itr.next();
   System.out.println(country);
}

Using ListIterator 

ListIterator is also an interface specially introduced for List type of collections with some additional flexibility on operations than in Iterator. Since difference between Iterator & ListIterator is not the topic of this article, We will take this up in the concern topic. Now, let’s see how we will use this ListIterator to print the value of each element in our countriesList. For example, observe the below code.

ListIterator<String> itr= countriesList.listIterator();
while (itr.hasNext()) {
    String country = itr.next();
    System.out.println(country);
}

Using Lambda Syntax of Java 8 

We can print each element of our list countriesList using Lambda syntax as below:

countriesList.forEach(item->System.out.println(item));

Using Method Reference of Java 8  

For example, here is the other simplified way to print in Java8 is by using Method Reference as below:

countriesList.forEach(System.out::println);

Using forEach() method of Stream in Java 8 

If your elements are in form of Stream, let’s see how can we print them. For example, observe the below code using Stream API in Java 8.

countriesList.stream().forEach(item->System.out.println(item));

                         OR

countriesList.stream().forEach(System.out::println);

 

Putting All together in a Single Program 

import java.util.Arrays;
import java.util.List;

public class TestPrintMethods {
    
    public static void main(String[] args) {
     
       // Creating a List traditionally using JDK-8 or lower versions
    /**   List<String> countriesList =
           Arrays.asList(
           "USA","China","UK","Canada","Russia","India","Australia"
          );
     */
       // Creating a List using Java 9 List.of() method 
         List<String> countriesList = 
          List.of(
           "USA","China","UK","Canada","Russia","India","Australia"
         );
      
       //Way#1: Using for loop 
       for (int i = 0; i < countriesList.size(); i++) {
          System.out.println(countriesList.get(i));
       }

       //Way#2: Using for-each loop
       for (String country : countriesList) {
          System.out.println(country);
       }

       //Way#3: Using Iterator
       Iterator<String> itr= countriesList.iterator();
       while (itr.hasNext()) {
          String country = itr.next();
          System.out.println(country);
       }

       //Way#4: Using ListIterator
       ListIterator<String> listItr= countriesList.listIterator();
       while (listItr.hasNext()) {
          String country = listItr.next();
          System.out.println(country);
       }

       //Way#5: Using Lambda Syntax of Java 8
       countriesList.forEach(item->System.out.println(item));

       //Way#6: Using Method reference of Java 8
       countriesList.forEach(System.out::println);

       //Way#7: Using forEach() method of Java 8 Stream API
       countriesList.stream().forEach(item->System.out.println(item));
       countriesList.stream().forEach(System.out::println);

    } 
}

Finally, we have learnt 7 ways to print elements of a collection in Java. Further, we can now decide the way which is best suited for our requirement. Additionally, If we find any other way to do it, we will update our article accordingly. Also, If you find any more, please don’t hesitate to put your comments.

 

close
Top