![](/img/trans.png)
[英]Java - selenium compilation error "cannot be resolved to a variable"
[英]Java streams collect function: property cannot be resolved [Compilation error]
這是示例代碼。 Collectors.toMap(...)中使用的prop正在顯示編譯器錯誤: [無法解析。
//Some code here
Object param1 = someValueAvail;
Map<String, someConfigPojo> configMap = someValueAvail;
List<String> propertyList = Arrays.asList("prop1","prop2");
Map<String, String> propertyMap = null;
propertyMap = propertyList.stream().filter(prop -> configMap.get(prop) != null)
.collect(Collectors.toMap(prop->prop, getValue(param1, configMap.get(prop).getPathAsString())));
// Some code here
private String getValue(Object param, String path){
//Do some stuff
return somevalue;
}
您需要另一個prop ->
:
Collectors.toMap(
prop->prop,
prop->getValue(param1, configMap.get(prop).getPathAsString()))
,
結束了lambda表達式prop->prop
,因此getValue
等是一個單獨的表達式,並且在范圍內沒有符號prop
。
注意prop->prop
也可以寫成Function.identity()
。
將您的valueMapper
更改為:
Collectors.toMap(Function.identity(),
prop -> getValue(param1, configMap.get(prop).getPathAsString()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.