In this section, we will learn what the Vector iterator() and listIterator() methods are and how to use them in Java.
What is Java Vector iterator() Method?
The Java Vector iterator() method is used to iterate through the elements of a vector object.
This method returns an object that implemented the Iterator interface.
This means we have these methods at our disposal after calling the iterator() method:
- forEachRemaining(): this method is like the forEach() method and using it we can create a set of instructions to be executed on every remained elements of a vector object.
- hasNext(): when iterating through a vector object, we can use this method to see if there’s still element left in the target vector object to iterate. Calling this method returns a Boolean value. If the result was true, that means there’s still an element in the target vector. Otherwise, if there was no element left, the return value of this method becomes false.
- next(): calling this method will give us the next element in the target vector.
- remove(): this method could be used to remove the next element in a vector object.
Java Vector iterator() Method Syntax:
public Iterator<E> iterator()
iterator() Method Parameters:
The method does not take an argument.
iterator() Method Return Value:
The return value of this method is an object that implemented the Iterator interface.
iterator() Method Exceptions:
The method does not throw an exception.
Example: using Vector iterator() method
import java.util.Vector; import java.util.Iterator; public class Main { public static void main(String[] args){ Vector<Integer> list = new Vector<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ int i = iterator.next(); System.out.print(i+", "); if (i >5){ iterator.remove(); } } System.out.println("\nHere are the elements of the vector after iteration: "); list.forEach(e-> System.out.print(e+", ")); } }
Output:
1, 2, 3, 4, 5, 6, 7, Here are the elements of the vector after iteration: 1, 2, 3, 4, 5,
As you can see, using the Iterator object, we can iterate a vector and remove its element if needed.
What is Java Vector listIterator() Method?
The Java Vector listIterator() method is used to iterate the elements of a list object.
The difference between this method and the iterator() method is that the return value from the listIterator() method is an object that allows us to iterate through the elements of a Vector backward as well.
Also, using the ListIterator object (the return object of this method) we can add or replace an element in a Vector object.
Java Vector listIterator() Method Syntax:
ListIterator<E> listIterator()
listIterator() Method Parameters:
The method does not take an argument.
listIterator() Method Return Value:
The return value of this method is a ListIterator object by which we can iterate through the elements of a vector backward and forward.
listIterator() Method Exceptions:
The method does not throw an exception.
Example: using Vector listIterator() method
import java.util.Vector; import java.util.ListIterator; public class Main { public static void main(String[] args){ Vector<Integer> list = new Vector<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); ListIterator<Integer> iterator = list.listIterator(); while(iterator.hasNext()){ int i = iterator.next(); System.out.print(i+", "); if (i >5){ iterator.set(i*50); } } System.out.println("\nHere are the elements of the vector after iteration: "); list.forEach(e-> System.out.print(e+", ")); } }
Output:
1, 2, 3, 4, 5, 6, 7, Here are the elements of the vector after iteration: 1, 2, 3, 4, 5, 300, 350,