
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you make a list iterator in Java?
List Iterators in Java are mainly useful for traversing a list in both directions (forward and backward). They allow you to iterate through the elements of a list while providing methods to modify the list during iteration.
In this article, we will learn how to create or make a list iterator in Java.
Ways to Create a List Iterator in Java
Using ListIterator Interface
We can utilize listIterator() method of the List interface, which allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides.
Example
In the below example, we will create a list of integers and then use the listIterator() method to create a list iterator for that list.
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class CreateListIterator { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); ListIterator<Integer> listIterator = list.listIterator(); while (listIterator.hasNext()) { Integer number = listIterator.next(); System.out.println("Number: " + number); } } }
Output
Following is the output of the above code:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
Using the Iterator Interface
Another way to create a list iterator is by using the Iterator interface. The Iterator interface provides methods to iterate over a collection, but it does not allow us to traverse the list in both directions.
Example
In the below example, we will create a list of integers and then use the iterator() method to create an iterator for that list.
import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class CreateListIterator { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer number = iterator.next(); System.out.println("Number: " + number); } } }
Output
Following is the output of the above code:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
Using Stream API
Stream API provides us with a way to create a list iterator using the Stream. We can use the stream() method to create a stream from a list and then use the forEach() method to iterate over the elements of the list.
Example
Let's create a list of integers and then use the stream() method to create a stream for that list.
import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class CreateListIterator { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Stream<Integer> stream = list.stream(); stream.forEach(number -> System.out.println("Number: " + number)); } }
Output
Following is the output of the above code:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5