I have a simple snippet as below. I referred this
List<Document> list = new LinkedList<Document>();
FindIterable<Document> itr = collection.find(findQuery)
.forEach((Document doc) -> list.add(doc));
return list;
It compiles without any issues.
- I guess that we are telling compiler that
doc
is of typeDocument
. Why is it needed?
But If I do the below, it throws ambiguous error. I referred this But couldn't relate and understand exactly.
collection.find(findQuery).forEach(list::add);
Could anyone please explain why second statement is not working?
is there any better way of writing the first one [working one]?
Java version: 1.8.0_231
import statements:
import java.util.List;
import java.util.Optional;
import com.mongodb.client.FindIterable;
import org.bson.Document;
forEach
doesn't compile for me. unable to reproduce this. – Necrosis