How to iterate over a list in various ways in java / Different methods of list iteration in java

Iterating over a collection is a task that most developers are required to perform during their development task and most of us keep on using the same way which we have used earlier though there are many ways to do this.
In this post I am going to list down all those ways. We all might be familiar with these ways. Still, keep on reading to refresh!!!

First let’s initialize a list

List list=Arrays.asList(new String[]{"first","second","third","fourth", "fifth"});

Now let’s begin iteration:

Method 1: Using traditional for loop

		for (int count = 0; count < list.size(); count++) {
			System.out.println("Item:: " + list.get(count));

This method is mostly used by the developers so need not to say anything about it.

Method 2: Using advanced for loop(for-each loop)

		for (String s : list) {
			System.out.println("Item:: " + s);

Detail: This is again a widely used method and most of us are familiar with its working

Method 3: Using java.util.Iterator

		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println("Item:: " +;

Detail: This method uses an iterator to fetch the elements of a collection. Used primarily for iterating over a Set.

Method 4: Using forEach method of java.lang.Iterable interface (New in java8)

               list.forEach(new Consumer() {
			public void accept(String s){
				System.out.println("Item :: "+s);

Detail: forEach method of java.lang.Iterable interface is used to loop over a collection. This method takes a java.util.function.Consumer interface object which has an accept method. This method takes a single argument and returns nothing. In each iteration, we are passing an element to this method and printing it out to the console.
There is another variant of above method which uses Lambda expression as:

                        list.forEach(s -> {
				s = "Item :: " + s;

Detail: Since java.util.function.Consumerinterface is a Functional interface, we may use Lambda Expression where this interface type is expected as an argument. forEach method of java.lang.Iterableexpects a java.util.function.Consumertype in its accept method. Hence, we are passing Lambda Expression to perform the same task.

Method 5: Using streams introduced in java 8

       ->; {
			s = "Item :: " + s;

Detail: Similar to the above method, we are utilizing a stream over the list object. stream() method is a new addition to java.util.Collection interface in java 8 and is a default method which returns a instance. has a forEach method which takes a java.util.function.Consumer object as an argument. Rest functionality is the same as Method 4 above.

Let’s tweak in:

  1. Method 1 may throw ArrayIndexOutOfBoundsException if you do a get(count) and count exceeds size of list.
  2. Method 3 is primarily used for iterating over a set since, unlike a List, a Set does NOT have a get() method to get its individual elements.
  3. When using iterator always check the condition using hasNext method of iterator. Never check null on next() method (like if(!=null))since next will give you next element move the iterator to the next element and you will not be able to get the previous element.
  4. Method 4 uses forEach method of java.util.Iterable interface. This is a default method added in this interface since java8.
  5. Methods 4 and 5 may only be used when you are using java 8 for development.
  6. Method 5 utilizes to iterate over a collection and is a powerful feature introduced in java 8. Using streams we may completely cut down iteration code when an element meeting a certain condition is required out of collection. For Example, from an employee list, if I need to get an employee with a particular id. To know how, check this out.

Mark Your Impression

Close Menu