Author
Oleksandr Miadelets
Head of Developers Team at CodeGym

Java Iterator

Published in the Java Collections group
members
Hi! In today's lesson, we'll talk about Java Iterator. Imagine that you go to the library, and you want to find a particular book. How do you do it? If it’s non-fiction, there’s the Dewey Decimal System. If it’s fiction, they’re sorted in alphabetical order by their last name. Whatever you’re looking for, at some point, you have to go through the books one at a time until you find what you’re looking for. Whenever you have a collection of things in Java, whether it’s stored in an Array, an ArrayList, a Queue, or a List, how do you find particular value? At some point, you have to go through each element one at a time. This is what a Java iterator is for.How to Use a Java Iterator: A Brief Tutorial - 1

What is an Iterator in Java

What is an iterator in Java? It is a way of looking at each element in a collection. And by collection, we mean anything in the Collection class. This includes:
  • ArrayList
  • HashSet
  • LinkedHashSet
  • LinkedList
  • PriorityQueue
  • Vector
  • and many others…
It also includes the various interfaces for those classes, such as Set, List, Queue, Dequeue, and Sorted Set, to name a few. Iterator<E> is the public interface method of the Interface Iterator<E> class. It was brought about in Java 1.2 and replaced Enumeration as a way to examine sequential elements in a collection.

Why You Shouldn’t Use For Loops as an Iterator in Java

One of the first ways that everyone is taught to iterate through a collection in Java is for a loop. It looks like this:
class Main {
  public static void main(String[] args) {
    int exampleArray[] = new int[10];

    //fill array with data

    for(int x = 0; x < exampleArray.length; x++) {
      System.out.println("Content of element " + x + "is: " + exampleArray[x]);
    }
  }
}
The output would be a list that reads:

Content of element 0 is: 0
Content of element 1 is: 1
Content of element 2 is: 2
etc.…
This does have its uses, but what happens if the collection doesn’t store elements in an index-based system? For example, Sets have no order normally. So it’s advisable to get out of the practice of using a for loop as an iterator in Java and practice using the Iterator<E> class instead. Here are some java iterator examples.

How to use Iterator in Java

Here are some examples of how to use iterator in Java. When using the iterator class, there are three ways to traverse a collection. You can use a while() loop, a for() loop, and a forEach() loop. Note that this for loop is different from the one we talked about before. Here are the three different java iterator examples. First, let’s set up the Collection to iterate through.
import java.util.*; // imports ArrayList, Collection and Iterator

class Main {
  public static void main(String[] args) {
    Collection<String> example = new ArrayList<String>();

    example.add("Item 1");
    example.add("Item 2");
    example.add("Item 3");
    example.add("Item 4");
  }
}
This is a simple Collection made up of an ArrayList to which we loaded four items. Now let’s look at the three methods of using the Iterator class to traverse the ArrayList.

While() loop

Iterator<String> iterator = example.iterator();

while (iterator.hasNext()) {
   System.out.println("Element Value= " + iterator.next());
}
This while() loop uses the .hasNext() Boolean method of the Iterator class to check to see if there is a future element. If the precondition is true, then it proceeds. If it returns as false, then the loop ends. The key part here is that the .hasNext() and the .next() methods both do an initial check of the first element. If the Collection is empty and there is no first element, then the method returns false for the .hasNext() and will throw a NoSuchElementException for the .next() method.

For loop

for (Iterator<String> iterator = example.iterator(); iterator.hasNext();) {
  System.out.println("Element Value= " + iterator.next());
}
This looks like a more traditional for loop. It uses the .hasNext() method as the condition check and change portion. The initialization is the call to the Iterator.

For:Each loop

for (String s : example) {
  System.out.println("Element Value= " + s);
}
The For:Each loop is a for loop, but if you don’t know how to read it, it may be a little confusing. The syntax of a For:Each loop is for (data_type variableName : collectionName){ body}. This for:each loop does have a couple of drawbacks. First, it can only traverse the collection in one direction. Second, you have to iterate through each element. You can’t skip any of them. But as a convenient list iterator in Java, it’s the best option. On the plus side, the for:each loop is very easy to read and once you know it, it’s difficult to get wrong. If you’re wondering what the output of the three iterator loops is, they are all the same:

Element Value= Item 1
Element Value= Item 2
Element Value= Item 3
Element Value= Item 4 

How to use an Iterator in Java For Maps

Maps are a popular way to store data, but because they don’t extend Collection, you can’t use the previous iterators to directly traverse a map. So how do you use an iterator in Java to go through Maps and HashMaps? There are four good Java map iterator methods. We’ll cover them individually. First, let’s load a map with a series of values.
import java.util.*; //imports Map and HashMap

class Main {
  public static void main(String[] args) {
    Map<String, String> example = new HashMap<String, String>();

    example.put("alpha", "one");
    example.put("beta", "two");
    example.put("gamma", "three");

  }
}

Java Hashmap Iterator Method: forEach(action)

example.forEach((k,v) -> System.out.println("Key: "+ k + ", Value: " + v));
This method uses a lambda expression to iterate through. The lambda operator is the forEach() method, and the body prints out the values. This uses a multiple parameter operator. This is the fastest and cleanest method for map iterator in Java 8.

Java Hashmap Iterator Method: For:Each() Loop

for (Map.Entry<String, String> iterate : example.entrySet()) {
  System.out.println("Key: " + iterate.getKey() + ", Value: " + iterate.getValue());
}
This uses the For:Each syntax to call the entrySet() method to return a set that has the key and value as its elements. Additionally, when using the .Entry() method, the objects are only true while this iteration is occurring.

Java Hashmap Iterator Method: Map.Entry<k, v>

Iterator<Map.Entry<String, String>> iterator = example.entrySet().iterator();

while(iterator.hasNext()){
  Map.Entry<String, String> element = iterator.next();
  System.out.println("Key: " + element.getKey() + ", Value: " + element.getValue());
)
This method again converts the Map to a set to use the Collections Iterator and methods. For these three iterator methods, the return looks like this:
Key: alpha, Value: one
Key: beta, Value: two
Key: gamma, Value: three
Java Hashmap Iterator Method: keySet() and values()
for (String key : example.keySet()) {
  System.out.println("Key: " + key);
}
for (String value : example.values()) {
  System.out.println("Value: " + value);
}
This one returns the elements in a different way. It will first return all of the Keys in order and then all of the Values:

Key: alpha
Key: beta
Key: gamma
Value: one
Value: two
Value: three
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet