You could create a list of Object like List<Object> list = new ArrayList<Object>()
. As all classes implementation extends implicit or explicit from java.lang.Object
class, this list can hold any object, including instances of Employee
, Integer
, String
etc.
When you retrieve an element from this list, you will be retrieving an Object
and no longer an Employee
, meaning you need to perform a explicit cast in this case as follows:
List<Object> list = new ArrayList<Object>();
list.add("String");
list.add(Integer.valueOf(1));
list.add(new Employee());
Object retrievedObject = list.get(2);
Employee employee = (Employee)list.get(2); // explicit cast