Is there an equivalent in Java 1.5 to Enumerable.Cast() from C#? I want to assign an ArrayList<MyType> to a variable declared as List<Object>. Eclipse grumbles “Type mismatch: cannot convert from ArrayList<MyType> to List<Object>“. I was just using List and ArrayList without generics but the compiler warns about that, and I wanted to clean up some warnings.
Is there an equivalent in Java 1.5 to Enumerable.Cast() from C#? I want to
Share
It would be unsafe to cast that if you were going to add to the list. However, if you only want to fetch from the list you can use:
Or closer to
Enumerable.Castwould be to useIterable:It’s a shame that you even need to specify the wildcard here (I was surprised that you did) given that
Iterableis a “read only” interface.For a lot more information on Java generics, see the Java Generics FAQ.