what is the difference between List and ArrayList?
Why not just store the ArrayList object in the ArrayList variable just like we do for String, int, and other data types.?
You might have seen something like this:
here we are using a List as a type of variable to store an object of ArrayList class, created using new() operator. This is known as programming for the interfaces. In fact, everywhere you need to declare a reference variable, you should always use the supertype e.g. Map for passing HashMap, Set for passing HashSet and List for passing ArrayList, LinkedList or Vector. You should use interface as type on the return type of method, type of arguments etc as shown below? Now big question comes, Why should you do that?
The answer is to take advantage of Polymorphism. If you use interface than in future if the new implementation is shipped then you are not required to change your program. For example, any program written using List will work as expected whether you pass a LinkedList, Vector or ArrayList, because they all implements List interface, they obey the contract exposed by List interface.
The only difference comes in performance, which is actually on of the driver for change. In short, if you program using interface, tomorrow if a better implementation of your interface is available then you can switch to that without making nay further change on the client side (part of the program which uses that interface).
Similarly, you should use interface type on method arguments:
and use interface type on the return type of methods:
If you store ArrayList's object into a reference type of ArrayList, as shown below then your code will not work if you pass a LinkedList, because ArrayList IS NOT a LinkedList.
Why not just store the ArrayList object in the ArrayList variable just like we do for String, int, and other data types.?
- The main difference between List and ArrayList is that List is an interface while ArrayList is a class
- it implements the List interface, which also means that ArrayList is a subtype of List interface.In Java or any object oriented language, super type of variable can store an object of subtype. This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from super type.
Why store ArrayList object on List variable?
You might have seen something like this:
List<Movie> listOfMovies = new ArrayList<Movie>()
here we are using a List as a type of variable to store an object of ArrayList class, created using new() operator. This is known as programming for the interfaces. In fact, everywhere you need to declare a reference variable, you should always use the supertype e.g. Map for passing HashMap, Set for passing HashSet and List for passing ArrayList, LinkedList or Vector. You should use interface as type on the return type of method, type of arguments etc as shown below? Now big question comes, Why should you do that?
The answer is to take advantage of Polymorphism. If you use interface than in future if the new implementation is shipped then you are not required to change your program. For example, any program written using List will work as expected whether you pass a LinkedList, Vector or ArrayList, because they all implements List interface, they obey the contract exposed by List interface.
The only difference comes in performance, which is actually on of the driver for change. In short, if you program using interface, tomorrow if a better implementation of your interface is available then you can switch to that without making nay further change on the client side (part of the program which uses that interface).
Similarly, you should use interface type on method arguments:
public void sort(List<? extends Comparable> input){ // logic to sort the list }
and use interface type on the return type of methods:
public List<Employee> getEmployees(int departmentId){ // logic to return employees for a given department }
If you store ArrayList's object into a reference type of ArrayList, as shown below then your code will not work if you pass a LinkedList, because ArrayList IS NOT a LinkedList.
but following code will work, even if you pass ArrayList, Vector, CopyOnWriteArrayList or LinkedList, because it expects a List, which is an interface:public List<Movies> getMovies(LinkedList<String> actors){ // this code will not work if you pass ArrayList }
public List<Movies> getMovies(List<String> actors){ // this code will work if you pass ArrayList, Vector etc }