Home » Java Tutorial » Java Iterator with Examples

Java Iterator with Examples

Hello readers! We’re back with another article that will help you in any way. Many of you might not know about the technical term of java iterator. So, in this article, we’re going to represent what does iterator in java mean, its syntax, different procedures, etc. that will calm all your rising questions in your mind. So, let’s not waste time and have a deep look at today’s article.

What the technical term “java iterator” exactly mean?

The iterator java mechanism portrays an object capable of initializing, one object at a time, through a list of Java objects. One of the oldest techniques in Java for iterating entity categories is the Iterator interface (yet not aged – enumerator predated Iterator).

In this concept, you would have to get an Iterator reference from the set of items you want to iterate over in an attempt to use an iterator in java. To guarantee you iterate over all of them, the retrieved Iterator keeps records of the entities in the underlying set.

The Iterator will reliably predict it when you change the fundamental collection when iterating through an Iterator pointing to that collection, and throw the exception the very next time you attempt to get the next element from the Iterator.

In general, there are mainly 2 types of iterators that are utilized in java. They are mentioned below:

An iterator

Iterator is a JDK 1.2 standardized iterator which can be used for any list. It can only be used for direct progress by Iterator. If the removal procedure is allowed, the element can be deleted using the iterator.

The ListIterator

ListIterator is an iterator that encourages bidirectional mapping for arrays of the list category.

How to iterate an Iterator in Java?

We need to get an Iterator from a list to proceed with; this is achieved by implementing the iterator() procedure.

We can get the Iterator instances from a list for elegance like below:

List<String> item = ….. Iterator<String>
Iterator_demo = item.iterator();

The interface of the iterator contains three main methods. They are as follows:

hasNext() method

This procedure is utilized to validate if there’s a minimum of one element which is left to iterate on over. The design of the hasNext() method is like a condition that is utilized in the while loop.

Iterator iterator_demo = list.iterator_demo();
 
while(iterator_demo.hasNext()) {
	Object nextObject = iterator_demo.next();
}

next() method

This next() procedure is utilized to jump on the next variable and retrieving it. Using hasNext() is a great exercise before trying to call the next(). Iterators for Sequences do not assure iteration in either specific order unless it is supported by a special implementation.

List<String> list1 = new ArrayList<>();
 
list1.add("Apple");
list1.add("Mango");
list1.add("Papaya");
 
Iterator<String> iterator1 = list1.iterator();
while(iterator1.hasNext()) {
	String value = iterator1.next();
 
    if(value.equals("Papaya")){
        list.add("chickoo");
	}
}

remove() method

This remove() procedure is utilized when you want to delete or remove the current variable that is present in the sequence.

List<String> list = new ArrayList<>();
 
list2.add("YOU");
list2.add("TAP");
list2.add("RAT");
 
Iterator<String> iterator2 = list2.iterator();
 
while(iterator2.hasNext()) {
	String value = iterator2.next();
 
    if(value.equals("TAP")){
    	iterator2.remove();
	}
}

The complete example of java iterator

We have seen the above 3 methods used to construct the Java iterator. Now, we’re representing the complete java iterator example that’ll clear all your doubts regarding the iterator interface in java and will make a clear vision.

while(iter.hasNext()){
String next = iter.next();
System.out.println(next);
 
if("SUNDAY".equals(next)){
 iter.remove();
}
}

The order of iteration

How entities found in an iterator java is traversed depending on the element that the Iterator is equipped with. For illustration, an iterator extracted from a list would iterate the values locally stored in the list through the items of that list in just the same manner. On the other hand, an iterator extracted from a set does not enable any commitments about the correct sequence in which the entities in the set are iterated.

Java list iterator

The utilization of the java list iterator has enhanced nowadays. This method is very useful when working with the iterators in java. It is represented as follows:

List list3 = new ArrayList();
 
list3.add("APPLE");
list3.add("MANGO");
list3.add("PAPAYA");
 
Iterator iter = list.iterator();

Java set iterator

We have represented below how to utilize the set iterator in java from a set entity. So, let’s have a quick eye on its example. Here you go!

Set set1 = new HashSet();
 
set1.add("APPLE");
set1.add("MANGO");
set1.add("PAPAYA");
 
Iterator iterator = set1.iterator();

The forEachRemaining() process

The Java Iter forEachRemaining() process will iterate locally over all the entities residing in the Iterator, and a Java Lambda Function forwarded to forEachRemaining() as a parameter for each component request. An illustration of using the technique forEachRemaining() is as follows:

List<String> list4 = new ArrayList<>();
list4.add("APPLE");
list4.add("MANGO");
list4.add("PAPAYA");
 
Iterator<String> iterator = list4.iterator();
    	
iterator.forEachRemaining((element) -> {
	System.out.println(element);
});

Pros of using java iterators

● In any category class, we can use it.

● Endorse both the process of READ and Delete

● The whole cursor is Universal.

● The names of the techniques are straightforward and convenient to use.

Cons of using java iterators

● It does not support the CRUD operation, as it does not support the Update and Build Execution.

● The forward components of a collection class can be obtained, which is why this is a unidirectional pointer.

Key Points to remember

Please keep in mind that every iterative reference will eventually refer to an index just before the first variable index in a list.

We don’t construct Enumeration, Iterator, and ListIterator entities because they’re interfaces. For entity formation, we use methodologies such as elements(), iterator(), listIterator(). These methodologies have unknown inner classes that exceed the associated interfaces and retrieve the item of this class.

Do you know?
1. Java Scanner with Examples
2. Java Stack with Examples
3. ParseInt Java
4. Try Catch JAVA
5. For loop Java with Examples
6. Java vs Javascript
7. Java Iterator with Examples

Pin It on Pinterest