-1
我有字段Map<MyObject, String> myField
。我用它如下:如何在Map包裝中實現Iterable
myField.forEach((myObject, text) -> myMethod(myObject, text));
我想本場換到新的對象:
class MyFieldWrapper {
Map<MyObject, String> myField;
...
//methods do somethings with myField
...
}
如何在MyFieldWrapper實現Iterable爲:myFieldWrapper.forEach((myObject, text) -> myMethod(myObject, text));
?
地圖不是可迭代的,如果您希望能夠在包裝上使用forEach(),則添加一個調用映射的forEach方法的forEach方法。它應該有相同的簽名,即採取BiConsumer <?超級MyObject,?超級字符串>作爲參數。 –