I am running into an issue in java 8 where it is not allowing me to collect objects when using a cast.
definitions.stream()
.map(Definition.class::cast)
.map((Definition definition) -> {
WonderfulDefinition wd = new WonderfulDefinition();
wd.name(definition.getName());
//etc
return wd;
}).collect(Collectors.toList())
And I am getting the compiler error:
Error:(71, 23) java: incompatible types: inference variable T has incompatible bounds equality constraints: java.util.List lower bounds: java.lang.Object
Any help would be appreciated.
Updated:
import java.util.*;
import java.util.stream.*;
class Driver {
public static void main(String[] args) {
List definitions = new ArrayList<>();
definitions.add(new Definition());
definitions.add(new Definition());
List<WonderfulDefinition> list =
definitions.stream()
.map(Definition.class::cast)
.map((Definition definition) -> {
WonderfulDefinition wd = new WonderfulDefinition();
wd.name(definition.getName());
//etc
return wd;
}).collect(Collectors.toList());
System.out.println(list);
}
}
class Definition {
private String name;
public String getName() {
return name;
}
public void name(String name) {
this.name = name;
}
}
class WonderfulDefinition extends Definition {
}
definitions
list ? – Schidu Luca