Declaring a LinkedList in Java

前端 未结 11 2003
北荒
北荒 2021-02-20 14:26

I always learn when we declare a collection we should do, Interface ob = new Class(), if i want to use for example a LinkedList i\'ll do List ob = new LinkedL

11条回答
  •  别跟我提以往
    2021-02-20 14:33

    If you actually have a need to use methods that are not on the List interface, there is certainly nothing wrong with using LinkedList's API. The general rule of programming to the List interface recognizes that 1) it's pretty rare to need those methods, and 2) in most people's experience, it's way more likely that I discover I need to sort the list and/or use a lot of random access, and decide to switch to an ArrayList, than it is I need one of the methods only LinkedList has.

    It may be also that you could be programming to the Queue interface, if you find List isn't giving you what you need.

提交回复
热议问题