Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 stream support in MongoTemplate

Why don't we have java 8 stream support in MongoTemplate in spring-data-mongodb-1.8.2.RELEASE.jar ?

I see that stream support have bean added in MongoRepository interface but I use pure MongoTemplate.

like image 927
poyger Avatar asked Feb 23 '16 07:02

poyger


2 Answers

In short

There is stream support but without exposing Stream on MongoOperations.

Explanation

Spring Data Mongo has stream support by exposing CloseableIterator<T> stream(final Query query, final Class<T> entityType). It does not use the Stream type on MongoOperations because Spring Data Mongo supports Java back to 1.6. You can obtain the Stream object by using StreamUtils.createStreamFromIterator(Iterator<T>). StreamUtils takes care of closing the stream and releasing resources.

HTH, Mark

like image 92
mp911de Avatar answered Sep 22 '22 17:09

mp911de


Mark's answer is correct (and should stay the accepted answer). Maybe a few more details on why you don't find a Stream on the MongoTemplate:

The core reason that there's no Stream on the MongoTemplate level is that Spring Data MongoDB is still compatible with Java 6. So we can't use Java 8 types in method signatures of classes we provide. With repositories, it's a different story as that's user code we inspect at runtime and — if Java 8 is present — dynamically adapt to, e.g. by converting the CloseableIterator<T> into a Stream.

like image 32
Oliver Drotbohm Avatar answered Sep 24 '22 17:09

Oliver Drotbohm