3

I am confused with a design problem in Java. It realized many abstract containers under the interface Collection and provides the method hasNext() and Next() along with class Iterator. What is the drawback if I put these methods directly under interface Collection and then overrides it in each subclass:

For example, I have already realized Next(); hasNext() method under class ArrayList. So what I wrote is

ArrayList ArrList=new ArrayList()
if(ArrList.hasNext())
  new obj=ArrList.next(); 
}

returning the objects stored in ArrList.

So is it redundant to introduce Iterator class for the interface Collection? And what is the benefit to design ArrList.iterator(); if it's more covenient to set it up in interface?

Can I find any book to solve such oop-design problems(As the computer scientists described it)?

Thanks for your time.

Henry.L
  • 219
  • 5
  • 14
  • A collection is very different from an iterator although both are related to each other. Read about the [*iterator* pattern](https://en.wikipedia.org/wiki/Iterator_pattern). – 5gon12eder Feb 01 '15 at 09:21
  • For example, `Collection` has `Set` subinterface, it doesn't make sense if it had `hasNext`. – Maroun Feb 01 '15 at 09:21
  • You understand that that code block won't compile, right? Because `ArrayList` instances **don't** have `hasNext` or `next`. The iterator isn't "redundant," this is the *whole point* of iterators: To give you a means of *iterating* through a collection. – T.J. Crowder Feb 01 '15 at 09:23
  • @MarounMaroun So you mean that we have to reserve iterator in order to serve all those subinterface? – Henry.L Feb 01 '15 at 09:24
  • @T.J.Crowder Yes. What I want to ask is why did't the designers of Java language put these methods inthe interface but have to introduce intermediately an iterator.The code is just an assumed example.Thanks. – Henry.L Feb 01 '15 at 09:26
  • @5gon12eder So... is it related to design patterns and I should borrow a copy of that book? – Henry.L Feb 01 '15 at 09:28
  • 1
    Yes, that would be a good reading. – 5gon12eder Feb 01 '15 at 09:30
  • It is a pity that I can accept one best answer, so I made a summary below:(1)Iterators allow us to run many threads of transversing at the same time.(2)Iterators also store info. about the current pointer which cannot be achieve in interface(3)Let's read Patterns of Design for more. Thank you all for helping me with this naive question. – Henry.L Feb 01 '15 at 09:33
  • possible duplicate of [Why is Java's Iterator not an Iterable?](http://stackoverflow.com/questions/839178/why-is-javas-iterator-not-an-iterable) – Joe Feb 01 '15 at 09:42

5 Answers5

3

The methods of the Iterator interface (next(), hasNext()) can't simply be added to the interface. An Iterator has a state which determines the next element that would be returned by the iterator.

If the Iterator methods were part of the Collection interface, you would need some additional method to reset this "built-in" iterator (in order to iterate again from the start of the Collection), and you would only have a single iterator for each Collection in any given time. A nested iteration as simple as the following snippet wouldn't be possible, since it requires two iterators :

List<Integer> list = ...
for (int i : list)
    for (int j : list)
        System.out.println(i+j);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • It is a pity that I can accept one best answer, so I made a summary below:(1)Iterators allow us to run many threads of transversing at the same time.(2)Iterators also store info. about the current pointer which cannot be achieve in interface(3)Let's read Patterns of Design for more. Thank you all for helping me with this naive question. – Henry.L Feb 01 '15 at 09:37
1

Iterator stores a pointer to some element inside a collection. In case of ArrayList it is an index of the underlying array.

It allows you to say iterate over the collection in two separate threads simultaneously. If the pointer was a part of ArrayList, each of the threads would skip some of the elements.

bedrin
  • 4,458
  • 32
  • 53
1

An iterator is usually made to traversed once. In the Java collection library classes will fail if modifications are made to the underlying collection during a traversal of an iterator.

BTW, this question may be more appropriate for Programmers Stack Exchange which is dedicated to theoretical programming questions.

Community
  • 1
  • 1
Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40
1

Let's assume for a moment that ArrayList did have hasNext and next methods, and so your code would compile. (You'd also need another method to tell the list you wanted to start over again.) That would mean that I could only have one iteration of the list active at a time, because the list itself contains the iteration state. That's just poor design; instead, we have the Iterator concept so that the state of the iteration is stored in the iterator, not the list, and we can have multiple iterators.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Yep,so I can use iterators to run many threads of traversing the list at the same time.It sounds sensical! Thank you! – Henry.L Feb 01 '15 at 09:31
1

At the conceptual level: Collection represents a collection of objects. Adding methods for hasNext and next would turn it into a collection of objects along with another piece of state, a 'current object', as well as some concept of how to traverse the collection.

Since these are two separate ideas, it is best to divide them into separate structures that are implemented independently. In the case you speak of, that would be the Collection structure (which handles storage and structure for a collection of objects), and the Iterator structure (which handles position and traversal of some collection of objects).

Kenogu Labz
  • 1,094
  • 1
  • 9
  • 20