How to cast generic List types in java?

后端 未结 9 995
离开以前
离开以前 2020-12-09 17:33

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

9条回答
  •  死守一世寂寞
    2020-12-09 18:35

    List is an interface, LinkedList is a concrete implementation of that interface. Much of the time an implicit cast will work, assign a LinkedList to a List, or pass it to a function expecting a List and it should just `work'.

    An explicit cast can also be done if necessary.

    //This is valid
    List myList = new LinkedList();
    
    //Also Valid
    List myList = (List) new LinkedList();
    

提交回复
热议问题