Iterator in Java

13,982

Solution 1

The Java collections are, as the name says, collections of things. If you don't know that word, look it up in a dictionary.

There are many types of collections. Take for example the mathematical concept of a set. You can put arbitrary things in a set, but it will never contain the same thing more than once. The things in the set are not ordered, that is you cannot say A comes before B. Another type of collection is a list. A list can contain the same thing more than once, and the order of the things in the list is important.

What all these collections have in common is that they contain things, which in Java are called elements. When you want to know which things are in a certain collection, you iterate over the collection, which is just another term for going through all elements. This is what an Iterator does. It basically starts at the beginning of a collection, you can always ask whether there is a next element (hasNext()), and if there is, you can get access to that element (next()), until you have iterated over all elements in the collection.

Solution 2

Technically iterators and collections are not directly related. However Iterators are mostly used together with collections to interate over the objects contained in the collection.

Iterators are a general and flexible concept that allows to interate objects without depending on the exact implementation of the entity that they iterate over.

Solution 3

An iterator is most commonly used as a mechanism for going through the elements of a collection.

The concept is not specific to Java at all, though the specific interface definition you show is.

See the Wikipedia article on Iterator for some discussion of what it means and how it's done in assorted languages including Java.

In Java, it is an Interface, so you can indeed implement your own, but sensible ones are defined for the collections in Java's collections library and for any Java Collection implementation the method

collection.iterator()

should return an iterator that will traverse the elements of that collection.

Also see the javadoc for Collection and Iterator for more.

Share:
13,982
Kevin
Author by

Kevin

Updated on June 04, 2022

Comments

  • Kevin
    Kevin almost 2 years

    What is Iterator and collections? Does these two have any relations?

    // the interface definition
    Interface Iterator {
        boolean hasNext();
        Object next(); // note "one-way" traffic
        void remove();
    }
    
    // an example
    public static void main (String[] args){
        ArrayList cars = new ArrayList();
    
        for (int i = 0; i < 12; i++)
            cars.add (new Car());
    
        Iterator it = cats.iterator();
    
        while (it.hasNext())
            System.out.println ((Car)it.next());
    }
    

    Does the Interface Iterator has these method names alone predefined or its user defined?. What does these four lines below actually tell?

    cars.add (new Car());
    Iterator it = cats.iterator();
    while (it.hasNext())
        System.out.println ((Car)it.next());
    

    Thanks. I am going through a book in collections.