3

Say I have something like

something.stream()
         .filter(filterMethod) // Same
         .map(mapMethod)       //
         .map(somethingElse)
         .filter(filterMethod) // Same
         .map(mapMethod)       //
         .filter(otherFilter)
         .filter(filterMethod) // Same
         .map(mapMethod)       // 

Could I create custom function on Stream and convert .filter().map() to one method? Implementing own Stream seems to overkill. It would be nice to have some short lambda function or method like

Stream<T> fooFiterMap(Stream<T> stream){
    return stream.filter(filterMethod).map(mapMethod);
}

and then reduce my something stream into

something.stream()
         .fooFilterMap()     // New
         .map(somethingElse)
         .fooFilterMap()     // New
         .filter(otherFilter)
         .fooFilterMap()     // New
11
  • 1
    You mean like something.stream().myFilterAndMap().myFilterAndMap().myFilterAndMap()? Commented Oct 10, 2017 at 18:00
  • That seems pretty hard to do. My guess is you would have to write your own Stream interface (extends Stream) to begin with. Commented Oct 10, 2017 at 18:15
  • @PaulLemarchand: if that is the question, then indeed it is, and then it's a duplicate of stackoverflow.com/questions/30685623/… :) Commented Oct 10, 2017 at 18:21
  • Possible duplicate of How to implement a Java stream? Commented Oct 10, 2017 at 18:21
  • 2
    yeah... you can't do that obviously without extending Stream in the first place (StreamEx library has done it for example); but looks like a big overkill here Commented Oct 10, 2017 at 19:53

1 Answer 1

2

You can obviously write your own one:

<T> Stream<T> fooFiterMap(Stream<T> stream, Predicate<T> predicate, UnaryOperator<T> function) {
    return stream.filter(predicate).map(function);
}

But the real question is why? It's too verbose? If so, than I'll argue - I like the chaining of filter and map more - bit it's subjective I guess. If you think about multiple objects created, than just think about the fact that these are probably stateless lambdas used on the same call-site, thus a single instance of Predicate and Function.

Sign up to request clarification or add additional context in comments.

4 Comments

The OP doesn’t want a method for combining an arbitrary Predicate and an arbitrary Function/UnaryOperator. The OP wants to repeatedly apply the same filter and map function, so that not specifying the functions again would truly shorten the code, though, I admittedly can’t imagine real life scenarios for that…
@Holger I do: your own snippet of filter(T.class::isInstance).map(T.class::cast)
@PostSelf how would T.class work? I don't get your example here
Probably not, I don't get this whole Java syntax, but you should get the meaning (filtering and downcasting).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.