Is it possible to convert a comma separated string to the list of java.lang.Enum by using stream?
My original code is the following, which is working:
List<String> inValuesStr = Arrays.asList(criteria.getValue().toString().split(","));
List<Enum> inValues = new ArrayList<>();
for (String val : inValuesStr){
inValues.add(Enum.valueOf(path.getType(),val));
}
I tried to refactor it to be as code below:
List<Enum> inValues = Arrays.stream(criteria.getValue().toString().split(","))
.map(v -> Enum.valueOf(path.getType(),v))
.collect(Collectors.toList());
Looks very basic... but, the following compile-time error is shown:
Error:(--, --) java: incompatible types: java.lang.Object cannot be converted to java.util.List<java.lang.Enum>
I can't understand where is the mistake. Did someone had the same experience? Thanks for sharing a solution.
eclipseissue may be? what doesjavacreport?criteria.getValue()to the list of specific type of enumpath.getType(). What is interesting is that if I remove abstraction, it worksList<Sex> inValues = Arrays.stream(criteria.getValue().toString().split(",")) .map(v -> Sex.valueOf(v)) .collect(Collectors.toList());But, I would like to keep it abstractList<Enum>toList<Type>