Sitemap

I Didn’t Understand Traversal in Java… Then One Small Example Changed Everything

4 min readOct 21, 2026

--

Press enter or click to view image in full size

When I first started learning Iteration and Traversal in Java, honestly, I didn’t understand the topic properly.

I knew the basics of ArrayList, and I had seen loops before. So I thought maybe this topic would be easy.

But when I started studying it on the internet, I realised that knowing the basics and actually understanding a concept are two different things.

The code looked easy, but I didn’t understand everything behind it.

The part that confused me the most

The biggest confusion for me was Iterator, especially these two methods:

  • hasNext()
  • next()

I kept trying to understand what these methods actually do and why we need both of them.

Then I decided to stop looking at complicated examples and try something simple.

Three names. One small program.

I took a simple example of printing names from a list.

Karthik
Arun
Priya

That’s it. Nothing complicated.

The idea was to visit each name in the list and print it one by one.

I started exploring different ways to do this in Java.

Iteration: Iteration is the process of repeatedly executing a block of code until a condition is satisfied or the required elements have been processed.

Traversal: Traversal is the process of visiting elements in a data structure, such as an array, list, set, or map, one by one.

Simple example: A company has three employees. Visiting each employee and displaying their details is traversal. Repeating the printing operation for each employee is iteration.

1. Traditional for loop

This was the easiest one for me to understand.

We use an index to access each element from the list.

for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}

Here, i starts at zero and increases until the loop reaches the end of the list.

This one made sense to me because I could clearly see how the elements were accessed.

2. Enhanced for loop

Next, I learned about the enhanced for loop.

for (String name : names) {
System.out.println(name);
}

This looked simpler because I didn’t need to manage an index.

Java visits each name in the list and prints it.

3. Iterator

This was the part I needed to spend more time understanding.

Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

Initially, hasNext() and next() confused me.

Get M.Karthikeyan’s stories in your inbox

Join Medium for free to get updates from this writer.

After working through the example, I understood the basic idea.

hasNext() checks whether another element is available.

next() returns the next element and moves the iterator forward.

Now I could understand why both methods were used together.

4. forEach()

I also learned about forEach(), another way to perform an action on each element.

names.forEach(name -> System.out.println(name));

Seeing these different approaches helped me understand that the same task can be performed in different ways in Java.

What actually helped me understand it?

For me, the biggest help was using small examples instead of trying to understand everything at once.

I tried the code, checked the output, and compared how each method worked.

When the program ran successfully and printed the names, I finally felt like I was getting the concept.

It may be a basic example for an experienced developer, but for someone who is learning, these small moments matter.

Where can we use this in real projects?

After understanding the basics, I started connecting the concept with real-world applications.

Think about an online shopping application. It needs to process products in a shopping cart.

An employee management system may need to display employee records.

A banking application may need to process transaction records.

In these situations, Java can traverse collections and perform the required operations on their elements.

That’s when I started seeing why this topic matters beyond just writing a loop.

One thing I want to remember

I used to think that if a code example looked simple, I should understand it immediately.

But that isn’t always how learning works.

Sometimes even a small concept needs time, practice, and a better example before it makes sense.

Preparing to explain this topic to others also made me realise something: when we try to explain a concept in simple words, we understand our own doubts better.

I’m not saying I’ve mastered Iteration and Traversal. I understand the basics now, and I still need more practice.

But at least now, Iterator, hasNext() and next() don't look as confusing as they did when I started.

Still learning, still improving

I’m continuing my Java Backend learning, one concept at a time.

There are many things I still need to learn, and that’s okay. I don’t want to just memorise code. I want to understand what it does and where it can be useful.

If you’re also learning programming and sometimes feel confused by even a basic topic, don’t worry too much about it.

Try a smaller example. Run the code. Observe the output. Give yourself time.

Sometimes, understanding one small example is enough to make a confusing topic feel much clearer.

Have you ever struggled with a simple programming concept until one small example finally made it click?

I’d love to hear about it.

--

--