public interface IPowerList<T> extends Iterable< T > {
public int bitFlag(List<T> subseq);
}
public class PowerList<T> implements IPowerList<T>{
private List<T> originalList;
private Set<Set<T>> powerList= new HashSet<Set<T>>();
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public int bitFlag(List<T> subseq) {
// TODO Auto-generated method stub
return 0;
}
}
Automatically I was given by Eclipse to implement an override method for iterator.
(1) Is it because I implement an interface that extends from iterable?
(2) My private variables are sets and lists- they have their own iterators, I don’t need to define any new iterator- so what should I do? should I erase the inheritance?
(3) when do we need to define a new iterator? when we define a class that it’s kind of collection for us? but I wonder why, cause we always use a generic collections, that have their own iterators..Can you give me an example?
(4) if I add to a set of sets a new HashSet<T>() will you consider it for adding an empty list or adding new HashSet().add(null) will be consider as an empty list?
Yes, you get the
iterator()method because your interface extendsIterable. That means that any class that implements your interface must also implement all the methods ofIterable. If you didn’t want that, don’t extendIterable.Your private members aren’t available to consumers of your class, so you need a way to expose those iterators. How you do that is up to you, but doing so through your
iterate()method makes a lot of sense.If you create your own collection that you want to be iterable, but that doesn’t extend a collection that already implements
Iterable, then you’ll need to create your own iterator. If you don’t care, then there’s no need, though your consumers might feel differently.