Get an iterator to specific element of a List in Java
This post will discuss how to get an iterator to the specific element of a List in Java.
1. Using a ListIterator
The standard way to get an iterator to a specific element of a list in Java is using the listIterator() method of the List interface. This method returns a list iterator over the elements in this list, starting at the specified position in the list. The specified index indicates the index of the first element that would be returned by an initial call to next(). An initial call to previous() would return the element with the specified index minus one. The following code demonstrates its usage, using the listIterator() method that takes an index of the first element to be returned from the list iterator.
|
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.Arrays; import java.util.List; import java.util.ListIterator; class Main { public static void main(String[] args) { List<String> names = new ArrayList<>( Arrays.asList("Liam", "Elizabeth", "Michael", "Benjamin")); int index = 2; ListIterator<String> it = names.listIterator(index); System.out.println(it.next()); // prints 'Michael' System.out.println(it.next()); // prints 'Benjamin' System.out.println(it.previous()); // prints 'Benjamin' System.out.println(it.previous()); // prints 'Michael' System.out.println(it.previous()); // prints 'Elizabeth' } } |
The ListIterator interface provides several methods to access and modify the elements of the list. For example, we can use hasNext() and hasPrevious() methods to check if there are more elements in either direction, and nextIndex() and previousIndex() methods to get the index of the next and previous elements. We can also use the add() method to insert a new element into the list between the element that will be returned by next() and the element that will be returned by previous().
|
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 28 29 30 31 32 33 34 35 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ListIterator; class Main { public static void main(String[] args) { List<String> names = new ArrayList<>( Arrays.asList("Liam", "Elizabeth", "Michael", "Benjamin")); int index = 2; ListIterator<String> it = names.listIterator(index); System.out.println(it.next()); // prints 'Michael' System.out.println(it.next()); // prints 'Benjamin' System.out.println(it.hasNext()); // prints false System.out.println(it.hasPrevious()); // prints true System.out.println(it.nextIndex()); // prints 4 System.out.println(it.previousIndex()); // prints 3 it.add("Emma"); // insert 'Emma' after 'Benjamin' System.out.println(names); // prints [Liam, Elizabeth, Michael, Benjamin, Emma] System.out.println(it.previous()); // prints 'Emma' System.out.println(it.previous()); // prints 'Benjamin' it.remove(); // remove 'Benjamin' System.out.println(names); // prints [Liam, Elizabeth, Michael, Emma] } } |
2. Using an Iterator
We can write a custom routine to advance the iterator to a specific position. This can be achieved by using List.iterator() that returns an Iterator to access each element sequentially in forward direction. This iterator only allows removing elements during iteration. Here’s an 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 28 29 30 31 32 33 34 35 36 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { public static <T> Iterator<T> iterator(List<T> list, int index) { if (index < 0 || list.size() < index) { throw new IndexOutOfBoundsException(); } Iterator<T> it = list.iterator(); while (index > 0) { it.next(); index--; } return it; } public static void main(String[] args) { List<String> names = new ArrayList<>( Arrays.asList("Liam", "Elizabeth", "Michael", "Benjamin")); int index = 2; Iterator<String> it = iterator(names, index); System.out.println(it.next()); // prints 'Michael' it.remove(); // remove 'Michael' System.out.println(it.next()); // prints 'Benjamin' System.out.println(it.hasNext()); // prints false System.out.println(names); // [Liam, Elizabeth, Benjamin] } } |
That’s all about getting an iterator to the specific element of a List in Java.
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 :)