Iterate Java List Using Loop, ForEach, Iterator and More

Jun 9, 2020

5 mins read

Published in

While programming array is essential to store list of fruits, list of employee, list of states and many more. Array has limitation and slow to travers through when the list of items are getting bigger and dynamically incresing.

AraryList in Java supports to store N numbers of items. After storing those items in the list, it needs iteration to find item which are available in the Array. We need to loop it one by one to fetch the required item.

Here are ways to Iterate or Loop List in Java.

1. For Loop

This is a basic for loop which we learn in Programming 101.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.ArrayList;
import java.util.List;

public class JavaLoopList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		for ( int i = 0; i < actorList.size(); i++ ) {
			System.out.println( "Actron Name:-> " + actorList.get(i) );
		}
	}
}

Output

--Using Foor Loop--

Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson

This code uses for loop which uses size() to get the available items in the List and going one by one to get the items from the list and print on the console.

2. Next Generation For Loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;
import java.util.List;

public class NextGenerationForLoop {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		for ( String actorName : actorList ) {
			
			System.out.println( "Actron Name:-> " + actorName );
			
		}
	}
}

Output

--Using Next Generation Foor Loop--

Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson

3. While loop: Programming 101

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.ArrayList;
import java.util.List;

public class JavaWhileLoopList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using While Loop--\n");

		int index = 0;
		
		while (index < actorList.size()) {
			
			System.out.println("Name:-> "+actorList.get(index));
			index++;
		}
	}
}

Output

--Using While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

4. Iterator with While Loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JavaIteratorWhileLoopList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using Iterator with While Loop--\n");

		Iterator<String> actoreIterator = actorList.iterator();
		
		while (actoreIterator.hasNext()) {
			
			System.out.println("Name:-> "+actoreIterator.next());
		}
	}
}

Output

--Using Iterator with While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

5. ListIterator with While loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class JavaListIteratorWhileLoopList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using ListIterator with While Loop--\n");

		ListIterator<String> actoreIterator = actorList.listIterator();
		
		while (actoreIterator.hasNext()) {
			
			System.out.println("Name:-> "+actoreIterator.next());
		}
	}
}

Output

--Using ListIterator with While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

6 Iterable.forEach()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.ArrayList;
import java.util.List;

public class JavaListIteratorForEachList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("-- Using Iterable.forEach() --\n");

		actorList.forEach((actorName) -> {
            System.out.println("Actor Name:-> "+actorName);
        });
		
	}
}

Output

-- Using Iterable.forEach() --

Actor Name:-> Robert Downey, Jr.
Actor Name:-> Tom Hanks
Actor Name:-> Leonardo DiCaprio
Actor Name:-> Will Smith
Actor Name:-> Tom Cruise
Actor Name:-> Dwayne Johnson

7 Stream.forEach

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;
import java.util.List;

public class JavaStreamForEachList {

	public static void main(String[] args) {

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

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("-- Using Stream.forEach() --\n");

		actorList.stream().forEach(
				(actrorName) -> System.out.println("Name:->" + actrorName ) );
		
	}
}

Output

-- Using Stream.forEach() --

Name:->Robert Downey, Jr.
Name:->Tom Hanks
Name:->Leonardo DiCaprio
Name:->Will Smith
Name:->Tom Cruise
Name:->Dwayne Johnson

Sharing is caring!