168

I've been using ArrayList recently in my android project at the office and I'm a bit confused between List and ArrayList, what is the difference of the two and what should I use?

Also I saw some implementations of it like.

List<SomeObject> myList = new ArrayList<SomeObject>();

ArrayList<SomeObject> myList = new ArrayList<SomeObject>();

What is the difference of those two instances?

Mat
  • 202,337
  • 40
  • 393
  • 406
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
  • 10
    Possible duplicate of [Type List vs type ArrayList in Java](http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java), [Java List vs ArrayList](http://stackoverflow.com/questions/9309093/java-list-vs-arraylist), and [List vs ArrayList](http://stackoverflow.com/questions/7726361/list-vs-arraylist); also, [List Vs. ArrayList](http://www.coderanch.com/t/379305/java/java/List-ArrayList), and [much more](https://www.google.com/search?hl=en&q=java+list+vs+arraylist)! – Cat Feb 15 '13 at 20:53
  • 3
    http://stackoverflow.com/questions/6867484/what-is-a-list-vs-an-arraylist?rq=1 –  Feb 15 '13 at 20:54
  • 3
    One is an interface, and the other one is a class. Now you can find it out on your own. – Rohit Jain Feb 15 '13 at 20:54

1 Answers1

178

There's no difference between list implementations in both of your examples. There's however a difference in a way you can further use variable myList in your code.

When you define your list as:

List myList = new ArrayList();

you can only call methods and reference members that are defined in the List interface. If you define it as:

ArrayList myList = new ArrayList();

you'll be able to invoke ArrayList-specific methods and use ArrayList-specific members in addition to those whose definitions are inherited from List.

Nevertheless, when you call a method of a List interface in the first example, which was implemented in ArrayList, the method from ArrayList will be called (because the List interface doesn't implement any methods).

That's called polymorphism. You can read up on it.

LarsH
  • 27,481
  • 8
  • 94
  • 152
ATrubka
  • 3,982
  • 5
  • 33
  • 52
  • 210
    List is not a class it is an interface. It doesn't have any methods implemented. So if you call a method on a List reference, you in fact calling the method of ArrayList in both cases. – robonerd Feb 15 '13 at 21:28
  • 4
    There is a difference, as @robonerd explained above. List is a Interface, you can type variables not only with classes or primitive types, but also with Interfaces. – Esteban A. Maringolo May 24 '14 at 00:49
  • 7
    Guys, please read my post carefully. Don't mix definition with implementation. In both cases implementation is the same: ArrayList. Variable is defined differently. Not sure what you're trying to change in my answer. – ATrubka Jun 02 '14 at 09:05
  • 1
    @ATrubka, In both the examples, If i call a method, which will execute the method in ArrayList class then what is the use of defining `List myList = new ArrayList();` – Nandan A May 15 '19 at 04:34
  • 2
    This way you decouple your code from specific implementation of the List interface. If later someone decides that LinkedList is better, then the only thing that changes will be the assignment. The above example is too trivial to demonstrate the benefits. Real life examples have more to it. For instance, there will be a method that takes List as a parameter rather than ArrayList. Take a look at Collections.sort() and other methods of the Collections class. – ATrubka May 16 '19 at 07:08
  • so which is better? – john k Jul 16 '21 at 21:49
  • In most cases declaring a variable as a List is more versatile. – ATrubka Jul 19 '21 at 16:01