Creating instance list of different objects

前端 未结 7 1483
自闭症患者
自闭症患者 2021-02-04 08:17

I\'m tring to create an arraylist of different class instances. How can I create a list without defining a class type? ()

List

        
相关标签:
7条回答
  • 2021-02-04 08:54

    I believe your best shot is to declare the list as a list of objects:

    List<Object> anything = new ArrayList<Object>();
    

    Then you can put whatever you want in it, like:

    anything.add(new Employee(..))
    

    Evidently, you will not be able to read anything out of the list without a proper casting:

    Employee mike = (Employee) anything.get(0);
    

    I would discourage the use of raw types like:

    List anything = new ArrayList()
    

    Since the whole purpose of generics is precisely to avoid them, in the future Java may no longer suport raw types, the raw types are considered legacy and once you use a raw type you are not allowed to use generics at all in a given reference. For instance, take a look a this another question: Combining Raw Types and Generic Methods

    0 讨论(0)
  • 2021-02-04 08:54

    If you can't be more specific than Object with your instances, then use:

    List<Object> employees = new ArrayList<Object>();
    

    Otherwise be as specific as you can:

    List<? extends SpecificType> employees = new ArrayList<? extends SpecificType>();
    
    0 讨论(0)
  • 2021-02-04 09:05

    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
    
    0 讨论(0)
  • 2021-02-04 09:07

    List anyObject = new ArrayList();

    or

    List<Object> anyObject = new ArrayList<Object>();

    now anyObject can hold objects of any type.

    use instanceof to know what kind of object it is.

    0 讨论(0)
  • 2021-02-04 09:16

    How can I create a list without defining a class type? (<Employee>)

    If I'm reading this correctly, you just want to avoid having to specify the type, correct?

    In Java 7, you can do

    List<Employee> list = new ArrayList<>();
    

    but any of the other alternatives being discussed are just going to sacrifice type safety.

    0 讨论(0)
  • 2021-02-04 09:16

    I see that all of the answers suggest using a list filled with Object classes and then explicitly casting the desired class, and I personally don't like that kind of approach.

    What works better for me is to create an interface which contains methods for retrieving or storing data from/to certain classes I want to put in a list. Have those classes implement that new interface, add the methods from the interface into them and then you can fill the list with interface objects - List<NewInterface> newInterfaceList = new ArrayList<>() thus being able to extract the desired data from the objects in a list without having the need to explicitly cast anything.

    You can also put a comparator in the interface if you need to sort the list.

    0 讨论(0)
提交回复
热议问题