I'm going through the intro to MongoDB for java. There's some example code to retrieve all the documents in a collection. The code works, but I find it a bit...clunky for lack of a better word. I'm wondering if there's a specific reason that makes it necessary. The given example is:
FindIterable<Document> iterable = db.getCollection("restaurants").find();
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document);
}
});
Is there some reason a Block
instance has to be created in every iteration of the forEach
in the above example? Why not something a little more straightforward like:
FindIterable<Document> iterable = db.getCollection("restaurants").find();
for (Document document : iterable) {
System.out.println(document);
}