Storing / reusing intermediate results on Java 8 streams

I have a list of A. to perform filtering, I need to map a to B. but once the filtering logic is completed, I still need a to perform further operations, so my question is whether it can achieve this? One way I can think of is to store both a and B in the third type, so I can use it when dealing with streams, but I'm not sure if it's elegant and wonder if it's a better way Or do I want to fit a square nail in a round hole by using a stream

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

Solution

Then you can always package two things 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 */)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>