List vs ArrayList in java

In this post, we will see the difference between List and ArrayList in Java.
List is an interface where ArrayList is concrete implementation, so List is more generic than ArrayList.

You can instance an ArrayList in below two ways.You might have seen this code before.

If you use 1st way, you can change the implementation later easily.
For example:
You need to change your implementation from ArrayList to LinkedList, you can simply change implementation, you don’t need to worry about making changes to all the code where variable list is being used.

You won’t be able to do it in case of 2nd way.

It is always a good idea to code to an interface rather than concrete implementation.

List vs ArrayList

Let’s see tabular difference between List and ArrayList in java.

ParameterListArrayList
ImplementationList is interface and does not have any implementationArrayList is concrete implementation and ArrayList implements List interface
MethodsList method can access methods which is available in interface.
For example:
It can not access method trimToSize because it available in ArrayList
ArrayList contains extra methods such trimToSize(), ensureCapacity() which is not in List interface.
Changing ImplementationYou can change implementation later.
For example:
You want to change implementation from ArrayList to LinkedList, you can do it.
You can not change implementation as ArrayList is already concrete implementation.
RecommendationList is preferred over ArrayList as you can change implementationArrayList over List not preferred as you have to stick with ArrayList.You won't be able to change implementation later.

that’s all about List vs ArrayList in java.


import_contacts

You may also like:

Related Posts

  • 10 September

    How to remove element from Arraylist in java while iterating

    Table of ContentsIntroductionUsing Collection’s removeIf() methodUsing ListIterator classUsing removeAll() methodUsing Java 8 Stream to filter List itemsConclusion Introduction In this tutorial, you will learn how to remove element from Arraylist in java while iterating using different implementations provided by Java. It is necessary to understand the right way to remove items from a List because […]

  • Print HashMap in java
    30 June

    Print HashMap in Java

    Table of ContentsPrint from the HashMap ReferencePrint HashMap using foreach method with keyset()Print HashMap using Consumer with entrySet()Print HashMap using Arrays’s asList() methodPrint HashMap using Collections’s singletonList()Print HashMap using getkey() and getValue with entrySet()Print HashMap using BiConsumerPrint HashMap using IteratorPrint HashMap using custom ObjectsConclusion In this article, we will see how to print HashMap in […]

  • Print LinkedList in java
    26 June

    Print LinkedList in java

    Table of ContentsJava inbuilt LinkedListPrint LinkedList using a for loopPrint LinkedList using Enhanced for loopPrint LinkedList using streamsPrint LinkedList using toString() methodPrint LinkedList using IteratorPrint LinkedList using custom objectsCustom singly LinkedListHow to print a custom singly LinkedListConclusion Java inbuilt LinkedList Java inbuilt LinkedList class uses doubly linked list as internal data structure to store its […]

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Print ArrayList in java
    11 May

    Print ArrayList in Java

    Learn about how to print ArrayList in java in different ways.

  • 15 April

    How to Deep Copy Arraylist in Java

    Learn about how to create deep copy of ArrayList in different ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.