Well, I have a class Customer (no base class).
I need to cast from LinkedList to List. Is there any clean way to do this?
Just so you know, I need to cast it
If your list is of a generic type for eg
ArrayList
then Following method should work
public List getListObjectInBodyOfType(Class classz, Object body) {
if(body instanceof List>){
List> tempList = (List>)body;
if(tempList.size() > 0 && tempList.get(0).getClass().equals(classz)){
return (List) body;
}
}
return new ArrayList();
}
How to use it?
List strList1 = getListObjectInBodyOfType(String.class, o);
as I mentioned before it works if the Object contains generic list, this won't work if you pass a non-generic list with mixed type of elements