1

Recently I was learning about Java 8 features so I started with a lambda expression, then I came across the Java stream API, now I am trying to wrap my head around the methods in stream API, How they work?

What I understood about lambda expression is if we want to pass the lambda expression to the method then we need to create an interface with the single unimplemented method (Java 8 interface can have default implementation) that matches the signature of a lambda expression. then the method to which the lambda expression is passed can execute the lambda expression by calling the interface method. So I can say that a lambda expression works as an implementation of the interface method.

But when I saw Stream API it has some methods which change the stream, for example below code just print integers by filtering integers which are less than 5.

import java.util.Arrays;
import java.util.List;


public class StreamDemo {
   static List<Integer> list= Arrays.asList(new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5), new Integer(6));
    public static void main(String[] ar) {
        list.parallelStream()
                .filter( x-> x<5)
                .forEach(System.out::print);
    }
}

But I am not understanding, how filter work as it does not have an implementation, I tried to see the implementation from Intellij which takes me to the Stream interface where the filter is non-implemented method which takes the parameter Predicate reference, but there is no method call of predicate method boolean test(T t); so how come Java evaluate the result, similarly there is method distinct which does not have an implementation but still gives me accurate result.

Thanks in advance.

3
  • 4
    filter does have an implementation, in this case it's provided by the Stream instance returned by parallelStream(). You can find all implementations of an interface method by clicking the little green down arrow in the margin next to the definition. Commented May 28, 2017 at 12:31
  • Thanks for the quick tip, but I am not able to navigate to distinct method definition Commented May 28, 2017 at 13:49
  • 1
    @AkshayNaik you don't need to know which class implements this method, and how it's implemented. All you need to know is that a Stream, whatever its concrete implementation is, has a distinct() method that works as documented in the javadoc of the Stream interface. Commented May 28, 2017 at 13:51

1 Answer 1

4

Stream is just an interface, like List. At runtime Java picks an implementation that does the job. If you wonder how filter method is implemented, you have an example in java.util.stream.ReferencePipeline#filter.

I hope this answers your question.

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

2 Comments

Thanks for the accurate answer, but it still leaves me with two question. 1. How the filter method is called without downcasting since stream is implemented by ReferencePipeline 2. ReferencePipeline does not have the implementation of distinct method
Okay, got it clearly now, the stream method gives the object of ReferencePipeline and since the methods are of stream interface no need to downcast the object. Thanks @jdebon

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.