我正在关注这个 guide尝试设置一个 mongoDB 数据库。
mongoClient.listDatabaseNames().forEach(System.out::println);
getDatabaseNames()
已弃用并被替换。
但是这一行给出了以下错误:
error: reference to forEach is ambiguous
mongoClient.listDatabaseNames().forEach(System.out::println);
^
both method forEach(Consumer<? super T>) in Iterable and method forEach(Block<? super TResult>) in MongoIterable match
where T,TResult are type-variables:
T extends Object declared in interface Iterable
TResult extends Object declared in interface MongoIterable
文档指出 listDatabaseNames() 返回一个 ListDatabasesIterable
,为什么我不能遍历这个列表?
最佳答案
您可以通过转换为 Consumer<String>
来帮助编译器解决歧义
mongoClient.listDatabaseNames()
.forEach((Consumer<String>) System.out::println);
关于java - 在 Java 中列出 mongoDB 的数据库时对 forEach 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47979978/