-2

I got a question in my mind that: What the difference 1) List list = new ArrayList()
2) List list = new LinkedList() ? No matter I initialize list in the both way above, still it can only use method in List, but not either ArrayList or LinkedList, isn't it?

I read from a website (http://www.corejavaguru.com/blog/java/new-arraylist), stated that it is known as "programming to an interface", which easy for a developer to change from ArrayList to LinkedList or vice versa in future. But no way for me to use method of ArrayList/LinkedList if I initialized in the way shown above, even it is flexible to change the way of initialize. I believe there is a reason. Hope to find answer for it.

vincent
  • 15
  • 1
  • 3
  • The rough difference is you should use `ArrayList`, you should not use `LinkedList`. I did the latter once in my 20 years as a Java programmer, and in retrospect I regretted. – Ole V.V. Oct 10 '18 at 13:47
  • 1
    @OleV.V. Why one should not use LinkedList? – Ivan Kaloyanov Oct 10 '18 at 13:49
  • 1
    I think the question is worded confusingly, but I believe that the OP is asking about the reasons for declaring the variable type as `List` rather than declaring the variable type as the actual concrete implementation class, either `ArrayList` or `LinkedList`. So I don't feel that this duplicates the linked question. – Bobulous Oct 10 '18 at 13:49
  • agree with @Bobulous. The title is misleading. – isaace Oct 10 '18 at 13:51
  • @IvanKaloyanov You can if you like, but why should you? In the 9 out of 10 cases where performance doesn’t matter, you can toss a coin. When it does matter, prefer `ArrayList`. I have never seen a case where `LinkedList` performed better, neither time-wise nor space-wise. Not even when insertions and deletions in the middle were sometimes done. – Ole V.V. Oct 10 '18 at 14:01
  • I read from website, initialize List list = new ArrayList() is better than ArrayList list = new ArrayList(), because of the flexibility (developer might want to change from ArrayList to LinkedList). But I wonder what the purpose to have the flexibility to change from ArrayList to LinkedList. Because with the initialize List list = new ArrayList() or List list = new LinkedList(), still we only can invoke method from List interface class. So to post at here to find the answer from professional around =) – vincent Oct 10 '18 at 14:04

1 Answers1

0

In both cases you created a variable named list with the type of List (Interface) as a pointer.

In the 1st case: List list = new ArrayList() You initiated the list variable with class ArryaList, which is an implementation of the List interface, in short:

Resizable-array implementation of the List interface

In the 2nd case: List list = new LinkedList() You initiated the list variable with class LinkedList, which is an implementation of the List interface, in short:

Doubly-linked list implementation of the List and Deque interfaces

In conclusion, in both cases you'll get a list which you can use according to the list interface definition. The main differences are:

  1. implementation it varies in terms of performance and memory usage
  2. each implementation might contain additional functionality when used in the specific implementation context
Alon Yampolski
  • 851
  • 7
  • 15