爲什麼我無法訪問Device
的id
屬性?爲什麼在使用Collectors.toMap時無法訪問類型字段?
final List<Device> devicesList = jsonFileHandlerDevice.getList();
ConcurrentMap<Integer, Device> map =
devicesList.stream()
.collect(Collectors.toMap(item -> item.id, item -> item));
其中
public class Device {
public MobileOs mobileOs;
public Integer id;
public Device() {
}
public Device(MobileOs mobileOs, double osVersion, int allocatedPort, Integer id, String uuid) {
this.mobileOs = mobileOs;
this.id = id;
}
}
在這裏看到:
我想也許你得到了一個誤導性的錯誤信息。當收集器返回的類型是Map'時,我相信實際的錯誤是使用'ConcurrentMap '類型。如果你希望返回的'Map'是一個'ConcurrentMap',你必須使用'toMap'變體來接受一個供應商(它決定了要返回的映射的類型)。 –
Eran
看我添加的截圖 –
什麼是錯誤信息? –