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" );
7 ways to print elements of a Collection in Java
Letโs explore 7 ways to print elements of a collection in Java one by one:
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.
You may go through a separate article on Collection In Java to know more about Collections & their features in Java.
ย