So, I came across a strange compilation error when using a generic class that has a List attribute (either Map or Set, etc.).
A compilation error occurs when trying to iterate (using foreach) List:
Sample.java:11: error: incompatible types for (String string : s.getStringList()) { required: String found: Object
Just to be clear, I know there is an easy way around the problem, but I want to understand what is wrong with the code
Below is the sample I created:
import java.util.List; public class Sample<T> { public List<String> stringList; public static void main(String[] args) { Sample s = new Sample();
java generics type-erasure
Jouwee
source share