In this post, we are going to see the main methods in the ListIterator interface, as well as their use. This adds some additional functionality and advantages over Iterator.
What is ListIterator?
ListIterator is an extension of Iterator that adds new functionality to iterate over lists. Obviously, it will inherit the main methods of Iterator and additionally incorporate some new ones that we will see below.
Main methods of use in the ListIterator interface.
To be able to use the ListIterator interface, we will start with a list and call the listIterator method.
List<String> numbers = List.of("1","2"); ListIterator<String> listIterator = numbers.listIterator(items.size());
hasNext() Method
The hasNext() method, which comes from the Iterator interface, is used to check if there is a next element in the collection.
while(listIterator.hasNext()) { ...... System.out.println(it.next()); }
hasPrevious() method in ListIterator
The hasPrevious() method is the equivalent of hasNext() but in reverse, it checks if there is a previous element.
while(listIterator.hasPrevious()) { //do something }
previous() in ListIterator
The previous() method retrieves the previous element from the ListIterator.
while(listIterator.hasPrevious()) { String previousElement = listIterator.previous(); }
Both hasPrevious() and previous() methods can iterate a collection in reverse order.
next() method
The next() method of a ListIterator allows us to move to the next element of the collection.
while(listIterator.hasNext()) { System.out.println(it.next()); }
nextIndex() method
The nextIndex() method of ListIterator indicates the index of the element that is returned by next().
String nextIndex = numbers.get(listIterator.nextIndex());
previousIndex() method
The previousIndex() method of ListIterator returns the index of the element when using previous().
String previousIndex = numbers.get(listIterator.previousIndex());
add() en ListIterator
The add() method in ListIterator allows us to add a new element just before the element returned by next() and after the element returned by previous().
listIterator.add("FOUR");
set() in ListIterator
The set() method allows us to replace the current element we are working with, i.e., we replace the element returned by the next() or previous() method call.
String nextElement = listIterator.next(); if( "1".equals(nextElement)) { listIterator.set("0"); }
remove()
To remove the current element in an Iterator, we use the remove() method.
while(listIterator.hasNext()) { Integer i = listIterator.next(); if(i < 5) { listIterator.remove(); } }
Main methods in the ListIterator interface
Método | Sintaxis | Descripción |
---|---|---|
hasNext() | boolean hasNext() | Check if the ListIterator has more next elements. |
hasPrevious() | boolean hasPrevious() | Check if the ListIterator has more previous elements. |
next() | public E next() | Get the next element of the ListIterator. |
previous() | public E previous() | Get the previous element of the ListIterator. |
nextIndex() | int nextIndex() | Get the index of the next element in the ListIterator. |
previousIndex() | int previousIndex() | Get the previous index in the ListIterator. |
remove() | void remove() | Remove an element from the ListIterator. |
add() | void add(E e) | Allows adding an element. |
set() | void set(E e) | Does a set of the element. |
Conclusion
In this post, we have seen the main methods in the ListIterator interface, which add more functionality to Iterator and improve the traversal we can perform on collections.
If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!