Iterate over a List in Java
This post will discuss various methods to iterate over a list in Java.
1. Using forEach() method
In Java 8 and above, we can use the forEach() method of the Stream API to perform an action for each element of a list. The idea is to call the stream() method to create a stream of elements, and apply a method to each element of the list using forEach(). This is similar to the enhanced for loop, but it uses a lambda expression or a method reference as an argument. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // 1. Get stream and use lambda expression list.stream().forEach(element -> System.out.println(element)); // 2. By providing method reference list.stream().forEach(System.out::println); // 3. List inherits `forEach()` from `Iterable` interface list.forEach(System.out::println); } } |
2. Using a loop
We know that list is an ordered collection, so we can access elements by their index in the list using a for-loop. This is a simple and straightforward way to iterate over a list, but it requires knowing the size of the list and using a loop variable. Alternatively, we can use an enhanced for loop or for-each loop to access each element directly. This is a more concise and readable way to iterate over a list, but it does not allow modifying the list or accessing the index of the element. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // 1. Print list using for-loop for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 2. Print list using enhanced for-loop for (String s: list) { System.out.println(s); } } } |
3. Using a ListIterator
The List interface provides a special iterator called ListIterator to access each element in either direction. This is a special type of iterator that allows iterating over a list in both forward and backward directions. A list iterator also provides methods to get the index of the current element, set or replace the current element, and insert or remove elements. We can call the List.listIterator() method to get a list iterator over the elements in a list. We can also use List.iterator() that returns an Iterator. For example:
|
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.Arrays; import java.util.Iterator; import java.util.List; import java.util.ListIterator; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // 1. Using `ListIterator` to iterate over a list ListIterator<String> lItr = list.listIterator(); // hasNext() returns true if the list has more elements while (lItr.hasNext()) { // next() returns the next element in the iteration System.out.println(lItr.next()); } // 2. Using Iterator Iterator<String> itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } } |
4. Using Enumeration Interface
Finally, we can iterate over a list in Java using the Enumeration interface. We can create an Enumeration object from the list using the enumeration() method of the java.util.Collections class. Then, we can use the hasMoreElements() and nextElement() methods of the Enumeration interface to check if there are any elements left in the list and get the next element respectively. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // `Collections.enumeration()` returns an enumeration over the specified collection Enumeration<String> enumeration = Collections.enumeration(list); while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } } } |
5. Using List.toString() method
To display the list’s contents to console or logs, we can use the toString() method of the List interface. This method returns the string representation of a list which consists of the list’s elements in the order they are stored in the list, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space) and each element is converted to a string using the String.valueOf() method. For example, the following code prints the string representation of the list using the toString() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("C", "C++", "Java"); // convert the list to a string using toString() String str = list.toString(); System.out.println(str); // [C, C++, Java] } } |
That’s all about iterating over a List in Java.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)