org.jhotdraw8.collection.mapped.MappedIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jhotdraw8.collection Show documentation
Show all versions of org.jhotdraw8.collection Show documentation
JHotDraw8 Utility classes for Collections
The newest version!
/*
* @(#)MappedIterator.java
* Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
*/
package org.jhotdraw8.collection.mapped;
import java.util.Iterator;
import java.util.function.Function;
/**
* Maps an {@link Iterator} to a different element type.
*
* The underlying iterator is referenced - not copied.
*
* @param the mapped element type
* @param the original element type
* @author Werner Randelshofer
*/
public class MappedIterator implements Iterator {
private final Iterator i;
private final Function mappingFunction;
public MappedIterator(Iterator i, Function mappingFunction) {
this.i = i;
this.mappingFunction = mappingFunction;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public E next() {
return mappingFunction.apply(i.next());
}
@Override
public void remove() {
i.remove();
}
}