10
votes

I'm struggling with evaluation expression that contains some Stream API methods. Example:

sample.reads.stream().filter(s -> s.l.length() < 10)

This doesn't work as well:

sample.reads.stream().filter(s -> s.l.length() < 10).collect(Collectors.toList())

gives:

enter image description here

However, sample.reads.stream().count() or sample.reads.stream().toArray() works fine as well as any other expression(and if I put stream API call into code it also works fine)

Config: Java: jdk1.8.0_144

Idea: IntelliJ IDEA 2017.3.1 (Community Edition) Build #IC-173.3942.27, built on December 11, 2017 JRE: 1.8.0_152-release-1024-b8 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0

UPDATE

Sorry, for confusion. Even terminal commands don't work. I showed this example because I found that examption is thrown on filter method, not on collect. If I add .collect() I'll see the same error

UPDATE 2 I don't think it's something with classes. Even this gives me the same error:

IntStream.range(0, 100).filter(i -> i%2 == 0).toArray()
2
You should give what is the type of sample, and the type of individual elements of sample.reads it is clear by the error that you miss some dependencies in the classpath - Hamdi Douss
@Hamdi Douss They are all my classes that are located in 'edu.gsu.molel' package. I don't think that it is the problem since 'sample.reads.stream().toArray()' works fine - Vyacheslav Tsivina
Please include the object schema on which you're trying to evaluate the lambda - andrewdleach
Are they in the same project or in a different project ? - Hamdi Douss
This seems to work just fine for me. I note that if I attempt to debug something in a running application that doesn't have the right classes imported, it will fail to do so with a similar error, but I'm usually given the option to "import" it in the debug window. Do you see any errors suggesting that you don't have access to the Collectors class? - Makoto

2 Answers

6
votes

This is a bit of a guess, but the problem seems to be a classpath related problem. Due to this you get the message that specific packages cannot be found.

I do not think this is due to terminal or non terminal stream operations. Even non terminal operations can be evaluated in IDEA without errors. See this:

enter image description here

I strongly suggest to check the Run/Debug configuration in IDEA and check which classpath is being used for this application. Open the following dialog and check what is in the "Use classpath of module" field:

enter image description here

A wrong value might have sneaked in there.

-1
votes

Look at the JAVA Stream API document here -

Stream operations are divided into intermediate and terminal operations, and are combined to form stream pipelines. A stream pipeline consists of a source (such as a Collection, an array, a generator function, or an I/O channel); followed by zero or more intermediate operations such as Stream.filter or Stream.map; and a terminal operation such as Stream.forEach or Stream.reduce.

Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.

Terminal operations, such as Stream.forEach or IntStream.sum, may traverse the stream to produce a result or a side-effect. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used; if you need to traverse the same data source again, you must return to the data source to get a new stream.

As it says, filter is a intermediate operation and not the terminal. In order to execute or complete the stream pipeline, you must have to have terminal operation. And hence, Intellij is giving the compilation error.

In case of sample.reads.stream().count() or sample.reads.stream().toArray(), count() & toArray() - both are terminal operations and hence it works fine.

To solve your problem, do something like this in Intellij expression evaluator -

sample.reads.stream().filter(s -> s.l.length() < 10).collect(Collectors.toList());

Here, collect() is the terminal operation & it will put the result in the list as mentioned by Collectors.toList().