Arraylist x List

Asked

Viewed 8,037 times

23

What is the difference of declaring ArrayList and List for lists in Java? What are the advantages of using one or the other?

2 answers

14


Program for interfaces

The first form is preferable whenever possible since it is more generic. So if you want to change the implementation, to a LinkedList or a custom list of your own, for example, it is still possible to do by maintaining code compatibility.

The rule is that you should declare the most generic type possible. Not that this needs to be done all the time, but whenever a change can affect compatibility. Depending on where it is declared, if it is something very internal, it may not affect anything, but if it is part of a public API it may affect.

This applies to any type, not just interfaces. The preference for interfaces is even more important since it improves encapsulation and uncoupling the design.

Due to polymorphism even if you state the second way you can use the created object anywhere that accepts a List, after all a ArrayList is a List at all times.

Testing

Another advantage is that it makes it easier to write tests this way. If the type is more general it is easier to switch the implementation to another one that performs the test more appropriately. Using the interface you facilitate the pattern of inversion of control.

Protecting

When you declare a parameter or as a member of a class, for example, using the interface, it is a way to make the method or member more general. When declaring a local variable is a way to protect yourself from using exclusive members of the implementation.

When you declare the variable with a type higher than the implementation you are telling the compiler that the object there can only do operations of this type and any attempt to access members of the specific type will produce an error even if the operation is possible.

Of course, using the more generic type you get a little limited. You cannot call all the methods available in the concrete implementation. That is why it is said that you should use the most general type "if possible". If you need these specific methods of the most specific type, then you have to use the second form.

Cost comparison

The memory consumption will be equivalent to the type whose implementation was used. That is, in the example will be the memory consumption will be the necessary for the ArrayList. In the example neither could be the consumption of Array even because the guy Array does not consume memory since interfaces have no state, so they do not use memory. But since they are not implementations they cannot be used to create objects.

On the other hand it is likely that the shape using the interface adds a slight extra processing consumption since there will be a indirect. It is not something important but it is good to know that there is this cost. But it depends on what you are comparing.

Other lists

Also consider the use of a type Collection which is even more general.

Generousness

A last note, this non-generic form is not recommended. Prefer to create lists with defined types, something like:

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

Completion

In short, this is a form of generalization. The advantages are the ease of maintenance (updating) of code and testing.

Some questions here that may be useful to better understand these things:

Interesting article to read.

  • I marked this as right to be more didactic and present more details

  • thank you very much for the clarification

  • I did not understand this part: "A last note. This non-generic form is not recommended." to which non-generic form you are referring?

  • @Math :D A used by Ricardo, below.

12

List is an interface. It defines the behavior of the Collections api lists.

Arraylist is a type of implementation of that list, as well as LinkedList is also an implementation of List.

When you define it this way:

List myList = new ArrayList();

You can only call methods and members that belong to List. If you define it as:

ArrayList myList = new ArrayList();

You will be able to call specific methods ArrayList and specific members use ArrayList beyond those inherited from the List.

Link to reply in English: Link

Text of Oracle with the implementations of List: Link

  • So when I use List I am using less memory than an Arraylist ?

  • I think so, since it is likely that Arraylist has its own attributes, (I also believe that the economy is tiny).

  • But then what is the real advantage of declaring a List instead of an Arraylist ?

  • 2

    may not be a real advantage, but you can have a method that takes as parameter a List, so it would be possible to pass an Arraylist and Linkedlist to this method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.