In this post, we will see the usage of the main Iterator methods in Java, how it behaves and how to use it for collections.
What is an Iterator in Java?
An Iterator in Java is an object that can be used to loop through a collection, such as a HashSet or an ArrayList. The Iterator object belongs to the java.util package.
Iterator was introduced in Java 1.2 as a replacement for Enumerations.
How to obtain an Iterator from a Collection?
To use Iterator, the first step is to obtain an Iterator element in Java, which can be done by calling the iterator() method. For example, let’s obtain an Iterator from a List in Java.
List<String> elements = new ArrayList<>(); Iterator<String> it = elements.iterator();
Once we have created our Iterator object, there are three main methods to work with it:
- hashNext(): Indicates if there are any elements left.
- next(): Iterates to the next element.
- remove(): Removes the current element.
hashNext() method in Iterator
The hasNext() method of an Iterator allows us to know if there are more elements in the Collection.
while(it.hasNext()) { ...... System.out.println(it.next()); }
next() in an Iterator
The next() method of an Iterator allows us to move to the next element of the Collection.
while(it.hasNext()) { System.out.println(it.next()); }
remove() method in an Iterator
To remove the current element in an Iterator, we use the remove() method.
while(it.hasNext()) { Integer i = it.next(); if(i < 5) { it.remove(); } }
Iterating in an Iterator in Lambda with forEachRemaining()
Java 8 introduced a new functionality to iterate through an Iterator with the arrival of Lambdas, which is the forEachRemaining() method.
iter.forEachRemaining(System.out::println);
Advantages of using Iterator
- Easy and simple to use, very intuitive methods.
- Read and delete.
- Can be used for any Collection in Java.
Disadvantages of using Iterator
- Not compatible with CRUD operations.
- You can only go in one direction using next.
- You cannot iterate in parallel, only sequentially.
- Not the best approach to use it on large volumes of data.
Summary of Iterator methods
Método | Tipo de Respuesta | Descripción |
---|---|---|
hashNext() | boolean | Returns true if there are more elements, false otherwise |
next() | E | Returns the next element of the Iterator. Throws NoSuchElementException if the iterator has no more elements |
remove() | void | Removes the current element from the Iterator. Throws ConcurrentModificationException if it is being modified |
forEachRemaining() | E | Runs a forEach on the entire iterator. If the order is specified, they are executed in order. Throws NullPointerException if the Iterator is Null. |
Conclusion of main Iterator methods in Java
In this post, we have seen the main Iterator methods in Java, as well as their usage, advantages and disadvantages.
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!!