You cannot share lambda areas. In other languages, you can use tuples, so instead of returning only the result, you return the result and the argument.
In java, you can create your own class to accommodate a pair of required data or create a Tuple to host a pair of data.
public class Tuple<A,B> { public final A _1; public final B _2; public Tuple(A a, B b){ _1 = a; _2 = b; } public static <A,B> Tuple<A,B> tuple(A a, B b){ return new Tuple<>(a, b); } }
import static so.alpha.Tuple.tuple;
importing a static tuple function like import static so.alpha.Tuple.tuple;
, you can map(tuple(d,f(d))))
, then your next function will be filter(t->p(t._1,t._2))
, and then you will map(t->t._1)
or if you add getters to the tuple, you can also map(Tuple::get_1)
So you can continue your d until the next step.
Stream<String> s = Arrays.asList("sa","a","bab","vfdf").stream(); Stream<Integer> result = s.map(d -> tuple(d.length(),d))
minus source share