在 Java 中列出 mongoDB 的数据库时对 forEach 的不明确引用

Car*_*ein 6 java lambda mongodb

我正在按照本指南尝试设置 mongoDB 数据库。

mongoClient.listDatabaseNames().forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

文档指出 listDatabaseNames() 返回 a ListDatabasesIterable,为什么我不能遍历这个列表?

Ous*_* D. 8

您可以通过强制转换为帮助编译器解决歧义 Consumer<String>

mongoClient.listDatabaseNames()
           .forEach((Consumer<String>) System.out::println);
Run Code Online (Sandbox Code Playgroud)