lambda foreach 添加到地图不起作用

lambda foreach add to map not working

我得到了以下变量

List<Pruefvorschrift> listP = new ArrayList<Pruefvorschrift>();
ObservableMap<TestDevice,List<Pruefvorschrift>> testDev = FXCollections.emptyObservableMap();

在一个函数中,我想使用 lambda 表达式填充 testDev

//first call REST service and get data
List<TestDevice> test_dev = call.getTestDevice("");
//now do a foreach to add each entry (as key) to the testDev ObservableMap with a empty List (as value)
test_dev.stream().forEach(td ->{
                    TestDevice t = td;                    
                    testDev.put(t, listP);
            });

但我得到的只是一个错误

java.lang.UnsupportedOperationException at java.util.AbstractMap.put(AbstractMap.java:209)

显然是这一行

 testDev.put(t, listP);

也许我误解了新流 api 但我只想用调用的所有结果(键)和一个空列表(稍后将修改的值)填充可观察映射。 有什么帮助吗?谢谢

无论 Map 类型由 FXCollections#emptyObservableMap

返回
FXCollections.emptyObservableMap();

不支持put方法。你不能给它添加任何东西。正如 javadoc 所述

Creates and[sic] empty unmodifiable observable list.

这与 lambda 表达式或 Stream api.

无关

只是在这里完成(Sotirios Delimanolis 绝对正确,而我大错特错 :)。我的问题已通过正确处理地图本身得到解决

//create empty map
Map<TestDevice,List<Pruefvorschrift>> map = new HashMap<TestDevice,List<Pruefvorschrift>>();
//use this map to create the ObservableMap
ObservableMap<TestDevice,List<Pruefvorschrift>> testDev = FXCollections.observableMap(map);

所有作品...Thx Sotirios