Java Generics: List, List<Object>, List<?>

后端 未结 12 1881
你的背包
你的背包 2020-11-22 17:08

Can someone explained, as detailed as possible, the differences between the following types?

List
List
List


Let me m

相关标签:
12条回答
  • 2020-11-22 17:56

    I will try to answer this in detail. Before generics we were having only List (a raw list) and it can hold almost anything we can think of.

    List rawList = new ArrayList();
    rawList.add("String Item");
    rawList.add(new Car("VW"));
    rawList.add(new Runnable() {
                @Override
                public void run() {
                   // do some work.
                }
            });
    

    The major problem with the raw list is when we want to get any element out of such list it can only guarantee that it would be Object and for that reason we need to use casting as:

       Object item = rawList.get(0); // we get object without casting.
       String sameItem = (String) rawList.get(0); // we can use casting which may fail at runtime.
    

    So conclusion is a List can store Object (almost everything is Object in Java) and always returns an Object.

    Generics

    Now lets talk about generics. Consider the following example:

    List<String> stringsList = new ArrayList<>();
    stringsList.add("Apple");
    stringsList.add("Ball");
    stringsList.add(new Car("Fiat")); //error
    String stringItem = stringsList.get(0);
    

    In the above case we cannot insert anything other than String in stringsList as Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. And we get error when we try to insert a Car instance in it. Also it eliminates cast as you can check when we invoke get method. Check this link for understanding why we should use generics.

    List<Object>

    If you read about type erasure then you will understand that List<String>, List<Long>, List<Animal> etc. will be having different static types at compile time but will have same dynamic type List at run time.

    If we have List<Object> then it can store only Object in it and almost everything is Object in Java. So we can have:

     List<Object> objectList = new ArrayList<Object>();
     objectList.add("String Item");
     objectList.add(new Car("VW"));
     objectList.add(new Runnable() {
            @Override
            public void run() {
    
            }
     });
     Object item = objectList.get(0); // we get object without casting as list contains Object
     String sameItem = (String) objectList.get(0); // we can use casting which may fail at runtime.
    

    It seems List<Object> and List are same but actually they are not. Consider the following case:

    List<String> tempStringList = new ArrayList<>();
    rawList = tempStringList; // Ok as we can assign any list to raw list.
    objectList = tempStringList; // error as List<String> is not subtype of List<Obejct> becuase generics are not convariant.
    

    You can see we can assign any list to raw list and major reason for that is to allow backward compatibility. Also List<String> will be converted to List at run time due to type erasure and assignment will be fine anyways.

    But List<Object> means it can only refer to a list of objects and can also store objects only. Even though String is subtype of Object we cannot assign List<String> to List<Object> as generics are not covariant like arrays. They are invariant. Also check this link for more. Also check the difference between List and List<Object> in this question.

    List<?>

    Now we are left with List<?> which basically means list of unknown type and can refer to any list.

    List<?> crazyList = new ArrayList<String>();
     List<String> stringsList = new ArrayList<>();
     stringsList.add("Apple");
     stringsList.add("Ball");
     crazyList = stringsList; // fine
    

    The character ? is known as wildcard and List<?> is a list of unbounded wildcard. There are certain points to observe now.

    We cannot instantiate this list as the following code will not compile:

    List<?> crazyList = new ArrayList<?>(); // any list.
    

    We can say a wildcard parameterized type is more like an interface type as we can use it to refer to an object of compatible type but not for itself.

    List<?> crazyList2 = new ArrayList<String>();
    

    We cannot insert any item to it as we don't know what actually the type would be.

    crazyList2.add("Apple"); // error as you dont actually know what is that type.
    

    Now question arises When would I want to use List<?>?

    You can think of this as a read-only list where you don't care about the type of the items. You can use it to invoke methods like returning the length of the list, printing it etc.

     public static void print(List<?> list){
            System.out.println(list);
        }
    

    You can also check the difference between List, List<?>, List<T>, List<E>, and List<Object> here.

    0 讨论(0)
  • 2020-11-22 18:02

    The shortest possible explanation is: The second item is a list that can hold any type, and you can add objects to it:

    List<Object>
    

    The first item you list is treated as essentially equivalent to this, except you will get compiler warnings because it is a "raw type".

    List
    

    The third is a list that can hold any type, but you cannot add anything to it:

    List<?> 
    

    Basically, you use the second form (List<Object>) when you truly have a list that can contain any object and you want to be able to add elements to the list. You use the third form (List<?>)when you receive the list as a method return value and you will iterate over the list but never add anything to it Never use the first form (List) in new code compiling under Java 5 or later.

    0 讨论(0)
  • 2020-11-22 18:03

    To add to the already good answers here:

    Method arguments:

    List<? extends Foo>

    good choice if you don't intend to alter the list, and only care that everything in the list is assignable to type 'Foo'. This way, the caller can pass in a List<FooSubclass> and your method works. Usually the best choice.

    List<Foo>

    good choice if you intend to add Foo objects to the list in your method. The caller may not pass in a List<FooSubclass>, as you intend to add a Foo to the List.

    List<? super Foo>

    good choice if you intend to add Foo objects to the list, and it's not important what else is in the list (ie, you are ok getting a List<Object> that contains a 'Dog' that has nothing to do with Foo).

    Method return values

    just like method arguments, but with the benefits reversed.

    List<? extends Foo>

    Guarantees that everything in the returned List has type 'Foo'. It might be List<FooSubclass> though. Caller cannot add to the List. This is your go-to choice and the most common case by far.

    List<Foo>

    Just like List<? extends Foo> but also allows the caller to add to the List. Less common.

    List<? super Foo>

    allows the caller to add Foo objects to the List, but does not guarantee what will be returned from list.get(0)... it could be anything from Foo to Object. The only guarantee is that this won't be a list of 'Dog' or some other choice that would prevent list.add(foo) from being legal. Very rare use case.

    I hope that helps. Good luck!

    ps. To sum up... two questions...

    do you need to add to the List? Do you care what is in the list?

    yes yes - use List<Foo>.

    yes no - use List<? super Foo>.

    no yes - use <? extends Foo> --- most common.

    no no - use <?>.

    0 讨论(0)
  • 2020-11-22 18:06

    List < Object > is meant to pass input type parameter of an Object. While List < ? > represents Wildcard type. The wildcard < ? > is of Unknown parameter type. The wildcard cannot be used as a type argument for a generic method and cannot be used to create a generic instance of a class. Wildcard can be used to extend a subtype class, List < ? extends Number >. To relax the restriction of an Object type and in this case to relax "Number" Object type.

    0 讨论(0)
  • 2020-11-22 18:07

    To complement the tutorials mentioned by Rob, here's a wikibook explaining the topic:
    http://en.wikibooks.org/wiki/Java_Programming/Generics


    Edit:

    1. No restrictions on type of items in list

    2. Items in list must extend Object

    3. Wildcard used by itself, so it matches anything

    Would it be naive of me to conclude at this point that there's hardly any/no difference at all?

    0 讨论(0)
  • 2020-11-22 18:08

    I'd put it this way: While List and List<Object> can contain any type of objects, List<?> contains elements of an unknown type, but once that type is captured, it can only contain elements of that type. Which is why it is the only type safe variant of those three, and therefore generally preferable.

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