I have a heterogeneous List that can contain any arbitrary type of object. I have a need to find an element of the List that is of a certain type. Looking through the answers of other generics related questions, I'm not finding exactly what I need.
Here's an example of what I'm trying to accomplish:
List <Object> list = new ArrayList <Object>();
...
private someMethod() {
Customer cust = findInList( Customer.class );
Invoice inv = findInList( Invoice.class );
}
So, how do I define findInList
using generics? I gather that type erasure causes issues here and I don't know as much about that as I probably should, but I'd rather not define multiple "find" methods since there could be dozens of different types of objects living in the List.