I am trying to convert the codes from native for loop to for each loop in lambda java 8. However, the index variable is not know. It is throwing an error that the variable must be final. The variable should increment as it creates dynamic data. How can I achieve it?
List<MetadataObject> metadatas = new ArrayList<>();
int index = 0;
dataIDs.forEach(id -> {
MetadataObject metadata = new MetadataObject();
metadata.setTypeKey(id);
metadata.setValue(metadataTypeValues.get(index));
metadatas.add(metadata); // this is unknown
index++; //this is unknown
});