On Wed, 8 Jan 2003, Stephen Colebourne wrote:
> So...
> public interface IterableMap extends Map {
> public MapIterator mapIterator();
> }
> public interface MapIterator extends Iterator, Map.Entry {
> // copied from extends
> public boolean hasNext();
> public Map.Entry next(); // implementation returns the map iterator, as
> that implements Map.Entry
> public Object remove();
> public Object getKey();
> public Object getValue();
> public void setValue();
> // on this interface
> public void reset();
> }
extends ResetableIterator.
> Thus:
> for (MapIterator it = map.mapIterator(); it.hasNext(); ) {
> it.next();
> doSomething(it.getKey(), it.getValue());
> }
Also create [MapIterator MapUtils.mapIteratable(Map)] which uses a hidden
implementation of MapIterator which wraps [map.entrySet().iterator()].
> The MapIterator interface could vary from that above. One alternative is to
> not extend Iterator or Map.Entry:
> public interface MapIterator {
> public boolean hasNext();
> public Object nextKey();
> public Object removeKey();
> public Object getValue();
> public void setValue();
> public void reset();
> }
> I think that the first proposal is more generally useful however.
I like the fact that someone can still use the first system as a normal
iterator, in which next() returns the object to be viewed. In the second
one, the Iterator idiom seems to be trickier. Then again, the second can
have a better performance and doesn't involve the highly painful
Map.Entry.
In this latter one, 'removeKey' is wrong. It should till be remove(), as
you remove the key and the value. The biggest problem with the second is
that it isn't Iterator. It could be:
public interface MapIterator extends ResetableIterator {
public boolean hasNext();
public Object next();
public Object remove();
public Object getValue();
public void setValue();
public void reset();
}
ie) Like calling Map.keys(), but with the additional power that you can
still do the value-thing. Biggest problem with this one is that it's not
as blatant that next() returns the key.
> Useful?
I think so. Can you JSR it up, get Map.Entry deprecated and one of these
interfaces put in place? :)
Hen
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>