Explain Codes LogoExplain Codes Logo

What is the difference between List and ArrayList?

java
polymorphism
arraylist
best-practices
Nikita BarsukovbyNikita Barsukov·Aug 21, 2024
TLDR
List<String> myList = new ArrayList<>();

A List is, in essence, an interface presenting a range of methods to manipulate sequences. In contrast, an ArrayList is a concrete class implementing this List interface using an array-based implementation, which is great for speedy random access and efficient modifications at the end of the list. It's good practice to use List for flexibility, and ArrayList when you require a reliable, performance-tuned solution.

Polymorphism in action

When we define a variable as a List, we're adopting the principle of polymorphism. This powerful concept in OOP lets us write code that doesn't care about the exact implementation, provided it's something that implements the List interface. This makes it simple to swap out an ArrayList for a LinkedList or a Vector without rewriting your entire code. Think of it as programming to an interface rather than a particular implementation.

Choosing List: More flexibility

Declaring your collection as a List means your future self (or any other developers who jump into your codebase) have the option to easily swap the ArrayList for a LinkedList or any other List implementation, should the context or performance requirements dictate. This design choice champions the Single Responsibility Principle and inevitably leads to easier maintenance and testability.

Specifics of ArrayList

While ArrayList is a realization of the List interface, it has its specificities. An ArrayList, for example, requires more memory for its capacity expansion buffer, and it's not synchronized, meaning it needs extra caution when used in a concurrent context. Keeping these factors in mind is crucial to optimize performance and avoid errors such as ConcurrentModificationExceptions.

Prefer ArrayList when...

Here's when you might want to use ArrayList:

  • Your collection size is fixed or grows rarely.
  • You insist on constant-time performance for get and set operations.
  • You don't have the need for synchronization in multithreaded scenarios. Unless you love surprises, of course!

Refactoring made easy

Imagine you started with an ArrayList, but then the requirements changed, and a LinkedList is the new hotness. If you’ve been using List, the refactor is a piece of cake:

List<String> myList = new LinkedList<>(); // just like switching coffee brands, smooth!

Your collection morphs effortlessly to fit your new requirements. This is a testament to the adaptability that List references offer.

Stick to the best practices

The well-established practice in coding is to minimize the visibility of your actual implementations — a key advantage of information hiding. This containment of behaviors in your classes guards against bugs and enhances readability. It's like using an opaque cup for your morning coffee; what matters is the caffeine, not the bean's origin!

Comparing ArrayList with others

To truly understand the difference, draw comparisons with other List implementations like a LinkedList or Vector. While LinkedList races ahead in add and remove operations at the list's start or middle, ArrayList shines bright in index-based operations. Such distinctions push List as the theoretical choice and ArrayList as the streetsmart choice where the benefits align with your needs.