Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between List<String> list = new ArrayList<String>() and ArrayList<String> list = new ArrayList<String>()? [duplicate]

Tags:

java

arraylist

Hi I have not a clear idea between,

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

and

ArrayList<String> list = new ArrayList<String>();
like image 221
user3300570 Avatar asked Jun 02 '14 09:06

user3300570


People also ask

What is the difference between List String and ArrayList String?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.

What is the difference between List String []> and List String >?

There's really no difference, although the second one will give you a compile warning. They compile to the same bytecode, because the compiler throws away the information about what type parameters you've used.

Is ArrayList and List same?

ArrayList class is used to create a dynamic array that contains objects. List interface creates a collection of elements that are stored in a sequence and they are identified and accessed using the index. ArrayList creates an array of objects where the array can grow dynamically.

Why do we do List new ArrayList?

List list = new ArrayList(); the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.


4 Answers

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

Creates a List of type string, and the list can be potentially typecast into any other type of list. This is called decoupling. You are decoupling your code from a specific implementation of the interface. The advantages it provides, is when writing large amounts of code, you can switch between types of lists to suit your preferences (speed, memory etc), as all of your code, can treat your list as just type List. You can also pass a List as parameters and returns List from functions. And later on if you are not happy with the ArrayList, you just change that one line of code

List<String> list = new ArrayList<String>(); // old code
List<String> list = new LinkedList<String>(); // new code

// The rest of the code doesnt need changing

...
list = getList();
...

public List<String> getList() {
  List<String> temporaryList;
  ...
  return temporaryList;
}

public void changeList(List<string> localListVariable) {}

And your program will behave as expected.


On the other hand,

ArrayList<String> list = new ArrayList<String>();

Creates an ArrayList of String types and it cannot be used as any other kind of List (Vector,LinkedList etc). Therefore it is bound by the methods available to an ArrayList. If you now want to change the type of list used, you will have to change all function parameters and return types and so on throughout your program (wherever you have had to create an ArrayList<String> to work with your variable).

like image 78
Husman Avatar answered Oct 05 '22 18:10

Husman


List<String> is a superclass (or could be a valid interface) of ArrayList<String>. Assigning an instance of ArrayList<String> to a List<String> variable is allowed. It's somehow a form of dynamic casting. When accessing List<String> list, only methods accessible with List<String> could be used; and those from ArrayList<String> would be hidden despite the object being an instance of ArrayList<String>.

like image 39
konsolebox Avatar answered Oct 05 '22 20:10

konsolebox


With respect to the instance in memory that you obtain there is no difference. However, it is considered a good practice to Program to Interfaces (see e.g. What does it mean to "program to an interface"?). Many Java APIs are defined in terms of interfaces, i.e. the functionality will work independently on which implementation of a particular interface you use. This aids code clarity and quality and reduces the probability of bugs which arise from relying on some particular properties of an implementation class (unless these properties are really important).

like image 31
Oleg Sklyar Avatar answered Oct 05 '22 18:10

Oleg Sklyar


In both the cases, you create object of ArrayList. But List<String> list = new ArrayList<String>(); will refer the object by using a reference of List<String> whereas ArrayList<String> list = new ArrayList<String>(); will refer the object by using a reference variable of type ArrayList<String>.

For example, you can call a method named ensureCapacity by using a reference of ArrayList<String>, but you can't do that using a reference of List<String>.

The following will compile:

ArrayList<String> list = new ArrayList<String>();
list.ensureCapacity(10);    // This will work

The following won't compile:

List<String> list = new ArrayList<String>();
list.ensureCapacity(10);    // This will not compile
like image 43
Rahul Bobhate Avatar answered Oct 05 '22 20:10

Rahul Bobhate