Suppose I have an Excel like data row class DataRow
and I would like to compare two rows and call an function to update one cell in a row if these two rows are different. My set up is currently this, using reflection to get each column's value (field):
try {
Field[] fields = oldRow.getClass().getDeclaredFields();
Arrays.stream(fields)
.forEach(field -> field.setAccessible(true))
.filter(field -> !field.get(oldRow).equals(field.get(newRow))
.findAny()
.ifPresent(newRow.updateACell())
.orElse(newRow.udpateACell("new value"))
} catch (Exception e){
System.out.println(e);
}
However, this code will give me an error because ifPresent
does not allow 'void' type here. I understand the function should be invoked upon the value ifPresent
receives, but is there anyway to achieve what I want to without using if else statements or for loop?