In my project, the external API we used is not genericfied, so there’s a class called ItemList which is an implementation of java.util.List, it holds a list of Item objects. however in our new code we express this as List<Item>, I want to write a method that can takes both ItemList and List<Item>,
I tried this kind of signature:
public static void readList(List<?> list) {}
it works fine, but the problem is there is a cast from Object to Item inside this method, which is used when the argument is ItemList, and not necessary for List<Item>, is there a better way to do this?
If
ItemListis an implementation ofList, and if you know it containsIteminstances, just cast it toList<Item>. You’ll get a type safety warning, but it’s not less safe than casting each element of theItemListtoItem.