如何在 java 中迭代 bson 文档

how to iterate in a bson document in java

我正在尝试遍历 java 中的 bson 文档,但出现错误

new Document().append("test",1).forEach(record -> {
        System.out.println(record);           ^ error here
});

我得到:

Error:(556, 49) java: incompatible types: incompatible parameter types in lambda expression, expected parameter 2 but found 1

当我尝试添加另一个参数时,一切都崩溃了

new Document().append("test",1).forEach(record, param2 -> {

由于Document implements the Map interface, that means it also inherits the default forEach method which takes a BiConsumer作为参数。因此你的 lambda 应该是这样的:

.forEach((key, value) -> { ... }