Yes, there is. It will be fully run-time solution (no compile-time type checking), but it's worth it (you will get an early crash on first attempt to insert an object with wrong type).
public class TypedArrayList extends ArrayList {
private final Class elementType;
public TypedList(Class elementType) {
this.elementType = elementType;
}
@Override
public boolean add(Object element) {
// preffered version, use it if it's available on your platform
// if (!elementType.isInstance(element)) {
if (!elementType.equals(element.getClass())) {
throw new IllegalArgumentException("Object of type " + element.getClass() + "passed to List of "+ elementType);
}
return super.add(element);
}
// override other methods
// that need type checking
}
Usage:
TypedArrayList list = new TypedArrayList(String.class);
list.add("works!");
list.add(new Object()); // throws exception
You can do the same for LinkedList and any other list type.