---
title: "Iterator in Java"  
description: "In this article I am explaining about Iterator in Java"  
author: "Manoj Pandey"  
published: 2015-04-14  
updated: 2017-12-05  
canonical: https://www.mindstick.com/articles/1721/iterator-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Iterator in Java

An iterator over a [collection](https://www.mindstick.com/articles/1718/collections-in-java). Iterator takes the place of [Enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) in the Java [Collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp) Framework. Iterators differ from enumerations in two ways:

· Iterators allow the caller to [remove elements](https://www.mindstick.com/forum/160320/how-you-can-check-if-an-element-exists-in-a-set-and-add-or-remove-elements-from-it) from the underlying collection during the iteration with well-defined semantics.

· [Method names](https://www.mindstick.com/forum/157800/what-happens-in-case-of-the-inherited-interfaces-have-conflicting-method-names) have been improved.

In computer [programming](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-programming), an iterator is an object that enables a programmer to traverse a [container](https://www.mindstick.com/forum/34127/extjs-how-to-set-border-for-the-container), particularly lists. Various types of iterators are often provided via a container's interface. We can say that it contain instance of collections may be [arrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp), List etc.

##### For example

```
  List myList=new ArrayList ( );  mylist.add("Data1");  mylist.add("Data2");  mylist.add("Data3");  mylist.add(1  mylist.add(2);
```

##### Iterator iter = mylist.iterator();

In Iterator have [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) useful methods which help us to getting item from list

· iter.hasNext()-: Returns true if the iteration has more elements.

· iter.next() -: Returns the next element in the iteration

##### Here I am creating a sample of iterator in java.

```
import java.util.ArrayList;import java.util.Iterator; public class Sample {     static ArrayList mylist;      public static void main(String args[]) {           mylist = new ArrayList();           mylist.add("Data1");            mylist.add("Data2");           mylist.add("Data3");           mylist.add(1);           mylist.add(2);           Iterator iter = mylist.iterator();            while (iter.hasNext()) {                Object item = iter.next();                System.out.println(item);           }                 } }
```

An iterator is an [abstraction](https://www.mindstick.com/articles/944/abstraction-in-c-sharp) over how a collection is implemented. It lets you go over the whole collection one item at a time, optionally removing items.

The disadvantage of an Iterator is that it can be a lot slow if you DO know the underlying implementation. For example using an Iterator for an ArrayList is considerable slower than just calling ArrayList.get(i) for each element. Whether this matters much depends on what the code is doing.

---

Original Source: https://www.mindstick.com/articles/1721/iterator-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
