Java 8 stream support in MongoTemplate

送分小仙女□ 提交于 2019-12-21 09:35:21

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/35571667/java-8-stream-support-in-mongotemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!