I have this Java method which his used to compare data:
org.apache.commons.lang3.builder.Diff;
public void addChangedPositions(DiffrentResult diffrentResult , List<UpdatedPositionsData> updatedPositionsData) {
for (Diff<?> diff : diffResult.getDiffs()) {
UpdatedPositionsData updatedData = new UpdatedPositionsData();
updatedData.setName(diff.getFieldName() == null ? null : diff.getFieldName());
updatedData.setOldValue(diff.getLeft() == null ? null : diff.getLeft().toString());
updatedData.setNewValue(diff.getRight() == null ? null : diff.getRight().toString());
updatedPositionsData.add(updatedField);
}
}
........
@Getter
@Setter
public class UpdatedPositionsData {
private String name;
private String oldValue;
private String newValue;
}
But for this line for (Diff<?> diff : diffResult.getDiffs()) {
I get error:
incompatible types: Object cannot be converted to Diff<?>
So I have:
Required type:
Object
Provided:
Diff
<?>
Do you know how I can fix this issue?
P.S unfortunately the problem is not solved.
I created this small example code:
Can you advise how can be fixed, please?
If you have a generic type, like DiffResult
, but you omit its generic type in the declaration, then all methods lose their generic information. In other words, if you declare the field, variable or parameter as DiffResult
instead of DiffResult<WhatEverType>
, then its getDiffs()
method doesn't return List<Diff<?>>
but instead List
- without any generic information.
This can be shown with a very easy example:
class MyClass<T> {
List<String> strings() {
return List.of("a", "b", "c");
}
}
MyClass m = new MyClass(); // raw type warning ignored
List<String> strings = m.strings(); // compiler warning
for (String s : m.strings()) { // compiler error, like the one you're getting
}
So my guess is, you've omitted the generic type in DiffResult
. If so, the fix is simple: add a generic type, or if you don't know or care, use the wildcard: DiffResult<?>
.