Storing/Reusing intermediate results on a java 8 stream

Pradyot :

I have a List of A , To execute filtering I need to map A to B. But once the filtering logic is done I still need A for further operations, So My question is would it be at all possible to achieve this? One approach I can think of is storing both A and B into a third type, so I have both available, while processing the stream, but not sure that is elegant and wondering if here is a better way.Or am I trying to fit a square peg in a round hole by using streams.

List<A> a;
List<B> b = a.stream().map(i -> load(i)).filter(need A here in addition to b)
Eugene :

Well you can always pass two things wrapped into a Pair, array, List for example:

a.stream().map(i -> List.of(load(i), i)) // List#of is java-9, replace with Pair or array
          .filter(x -> x[0]...)
          .filter(y -> /* y is List here */)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=462710&siteId=1