I'm following this guide to try and setup a mongoDB database.
mongoClient.listDatabaseNames().forEach(System.out::println);
getDatabaseNames() is deprecated and replaced.
However this line gives the following error:
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
The documentation states that listDatabaseNames() returns a ListDatabasesIterable, why can I not iterate through this list?
You can help the compiler resolve the ambiguity by casting to Consumer<String>
mongoClient.listDatabaseNames()
           .forEach((Consumer<String>) System.out::println);
                        listDatabaseNames() exposes to different forEach methods. One can receive as argument Block<? super String> block and the second one receive Consumer<? super String> consumer.
In order to avoid this ambiguity you will need to cast it to your needs.
  mongoClient1.listDatabaseNames()
              .forEach((Block<String>) System.out::println);
There is also an open issue about this here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With