Search code examples
javaalgorithmgenericsabstract-data-type

Do all ADTs in Java need to have iterators?


I was reading the book algorithms by Robert Sedgewick and I came across chapter 1.3 about ADTs and I have a few questions.

Are all ADT's in java iterable?

Does that mean we have to implement an iterator each time we implement an ADT?

If so, do I make a separate API for the iterator? Because in page 141 he made a interface just for the Iterator.

I was talking to a friend and he said "An instance of an ADT can be modified (i.e. elements added/deleted/or modified) when iterated" isn't this wrong because in a bag elements cannot be removed?


Solution

  • An abstract data type is nothing more than a set of operations together with the contract for these operations. Typically, there are several implementations of an ADT possible that may show different non-functional characteristics (e.g. run time of operations) or different bahaviour in areas that are not specified in the contract. It depends on the nature of the concrete ADT if iteration is one of the operations. It does not have to.

    In Java, you can only define the signatures of the operations (e.g. in an interface). The contract has to be given informally for example in the Javadoc.