Type List vs type ArrayList in Java

2018-12-31 03:36发布

(1) List<?> myList = new ArrayList<?>();

(2) ArrayList<?> myList = new ArrayList<?>();

I understand that with (1), implementations of the List interface can be swapped. It seems that (1) is typically used in an application regardless of need (myself I always use this).

I am wondering if anyone uses (2)?

Also, how often (and can I please get an example) does the situation actually require using (1) over (2) (i.e. where (2) wouldn't suffice..aside coding to interfaces and best practices etc.)

15条回答
笑指拈花
2楼-- · 2018-12-31 03:38

Out of the following two:

(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();

First is generally preferred. As you will be using methods from List interface only, it provides you the freedom to use some other implementation of List e.g. LinkedList in future. So it decouples you from specific implementation. Now there are two points worth mentioning:

  1. We should always program to interface. More here.
  2. You will almost always end up using ArrayList over LinkedList. More here.

I am wondering if anyone uses (2)

Yes sometimes (read rarely). When we need methods that are part of implementation of ArrayList but not part of the interface List. For example ensureCapacity.

Also, how often (and can I please get an example) does the situation actually require using (1) over (2)

Almost always you prefer option (1). This is a classical design pattern in OOP where you always try to decouple your code from specific implementation and program to the interface.

查看更多
柔情千种
3楼-- · 2018-12-31 03:39

List is an interface.It doesn't have methods. When you call method on a List reference. It in fact calls the method of ArrayList in both cases.

And for future you can change List obj = new ArrayList<> to List obj = new LinkList<> or other type which implements List interface.

查看更多
爱死公子算了
4楼-- · 2018-12-31 03:39

I would say that 1 is preferred, unless

  • you are depending on the implementation of optional behavior* in ArrayList, in that case explicitly using ArrayList is more clear
  • You will be using the ArrayList in a method call which requires ArrayList, possibly for optional behavior or performance characteristics

My guess is that in 99% of the cases you can get by with List, which is preferred.

  • for instance removeAll, or add(null)
查看更多
爱死公子算了
5楼-- · 2018-12-31 03:41

Actually there are occasions where (2) is not only preferred but mandatory and I am very surprised, that nobody mentions this here.

Serialization!

If you have a serializable class and you want it to contain a list, then you must declare the field to be of a concrete and serializable type like ArrayList because the List interface does not extend java.io.Serializable

Obviously most people do not need serialization and forget about this.

An example:

public class ExampleData implements java.io.Serializable {

// The following also guarantees that strings is always an ArrayList.
private final ArrayList<String> strings = new ArrayList<>();
查看更多
低头抚发
6楼-- · 2018-12-31 03:43

Almost always the first one is preferred over the second one. The first has the advantage that the implementation of the List can change (to a LinkedList for example), without affecting the rest of the code. This will be a difficult task to do with an ArrayList, not only because you will need to change ArrayList to LinkedList everywhere, but also because you may have used ArrayList specific methods.

You can read about List implementations here. You may start with an ArrayList, but soon afterwards discover that another implementation is more appropriate.

查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 03:44

When you write List, you actually tell, that your object implements List interface only, but you don't specify what class your object belongs to.

When you write ArrayList, you specify that your object class is a resizable-array.

So, the first version makes your code more flexible in future.

Look at Java docs:

Class ArrayList - Resizable-array implementation of the List interface.

Interface List - An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted.

Array - container object that holds a fixed number of values of a single type.

查看更多
登录 后发表回答