The problem is that you are combining RawType with a common one in your solution. You do this in the getZipList(boolean) method. Since List not of this type, you slow down type control. The next place you cheat the compiler is how you declare Iterator<?> , Since you are not declaring a generic parameter, I will store the object. So the next time you avoid the certainty that you are taking advantage of it to see where you are dropping the expected type. That is why his work. Casting is usually done by the compiler, but you implement it correctly yourself.
IF your code will look like this
Iterator<String> iter = list.iterator(); while (iter.hasNext()){ ZipCode zipplace = (ZipCode) iter.next();
IF your main method would look like this:
while(String s : list) { System.out.wirteln(s); }
A ClassCastException will be thrown. Because the code will "look like"
Iterator<Object> iter = list.iterator(); while (iter.hasNext()){ ZipCode zipplace = (String) iter.next(); System.out.println(zipplace.value); }
As in Java, you cannot use this method. Exception thrown.
To summarize, you created code that slows down type checking, but you have implemented its proper use. That is why his work.
source share