How do I iterate and perform some arbitrary operation on each item?
I have a Abstract Iterator class which has this function
void iterate(){
while(this.hasnext()){
..this.next()..
}
}
How do I pass in any arbit开发者_如何学编程rary function that will be applied to the next element. For example, is there a way to do iterate(print)
?
In Java, it is impossible to directly pass functions in as parameters. Instead you need to set up an interface with one method like so:
interface Operate {
public void operate(Object o);
}
Then, you need to implement the interface for each different operation you would like to perform on the Object
.
An example implementation.
class Print implements Operate {
public void operate(Object o){
System.out.println(o.toString());
}
}
Implementation in your code:
iterate(Operate o){
while(this.hasnext()){
o.operate(this.next());
}
}
I would reconsider the naming to clarify the semantics:
Having a method in the iterator:
public <T> Object[] apply (Action<T> action, Object context) {
List<T> results = new ArrayList<T>();
while(hasNext()) {
results.add(action.invoke(this.next(), context));
}
T[] r = new T[results.size()];
return results.toArray(r);
}
public interface Action <T> {
public T invoke (Object target, Object context);
}
Context can be null, but in some cases you really need some sort of pseudo closure to take actions that require context. To make this more robust, you can consider adding exception handling.
Are you assuming that print is a function? Java does not have function pointers or real closures or anything that lets you pass arbitrary functions to other functions, like you would see in more functional languages. You can fake this by creating a Function interface, which has one method,
void apply(Object arg)
You can then create different classes that implement Function and pass those into your iterate method. In your example, you would do something like this perhaps:
public class PrintFunction implements Function {
void apply(Object arg) {
System.out.println(arg.toString());
}
}
精彩评论