Java 8 Double function reference?

Viewed 80

Suppose I have this class:

class Dependency{      
   public Dependency reverse() { ... } // returns this Dependency after some reverse OP

   public int doSomething(int param) { ... }

}

Can I use it like this:

Optional<Integer> opt = ...
Dependency dep = new Dependency();

opt.map(dep::reverse::doSomething);

This would be quite clean, but is it possible? To apply a transformation (reverse) to Dependency and then apply the doSomething method?

1 Answers

If I understood you correctly, seems that reverse() won't be consuming the Integer, so you could simply do:

opt.map(dep.reverse()::doSomething);
Related