Java
Programming
Collection
List
Data Structures

What is the difference between Collection and List in Java?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, understanding the nuances between different data structures is essential for writing efficient and effective code. Two commonly encountered interfaces in the Java Collection Framework are Collection and List. While they may appear similar at first glance, they have distinct characteristics and use cases. Let's delve into the specifics of each and explore their differences through examples and explanations.

Collection Interface

Overview

The Collection interface is the root interface in the Java Collections Framework, providing the foundation from which other collection types, like List, Set, and Queue, derive. It encapsulates the concept of a group of objects known as elements.

Characteristics

  • Generic Aliasing: Supports generic methods that allow you to create type-safe collections.
  • Basic Operations: Provides core methods for collection manipulation, such as add(), remove(), size(), clear(), and contains().
  • No Positional Access: Does not guarantee order or positional access to its elements. Thus, you cannot retrieve elements based on position.

Example

Here's a simple example demonstrating Collection:

java
1import java.util.Collection;
2import java.util.HashSet;
3
4public class CollectionExample {
5    public static void main(String[] args) {
6        Collection<String> collection = new HashSet<>();
7        collection.add("Apple");
8        collection.add("Banana");
9        collection.add("Orange");
10
11        for (String fruit : collection) {
12            System.out.println(fruit);
13        }
14    }
15}

List Interface

Overview

The List interface extends the Collection interface and is a part of the Java Collections Framework. It is designed for storing ordered collections that allow duplicates.

Characteristics

  • Sequential Access: Guarantees order of insertion and allows positional access.
  • Duplicates Allowed: Supports duplicate elements, which is a key difference from some other Collection types like Set.
  • Positional Operations: Offers methods like get(int index), set(int index, E element), add(int index, E element), and remove(int index) for operations that rely on element order.
  • ArrayList vs LinkedList: Common implementations include ArrayList (dynamic array) and LinkedList (doubly linked list).

Example

Here's a simple example demonstrating List:

java
1import java.util.List;
2import java.util.ArrayList;
3
4public class ListExample {
5    public static void main(String[] args) {
6        List<String> list = new ArrayList<>();
7        list.add("Apple");
8        list.add("Banana");
9        list.add("Apple"); // Duplicates allowed
10
11        System.out.println(list.get(1)); // Retrieves element at index 1
12
13        list.set(2, "Orange"); // Replaces element at index 2
14        for (String fruit : list) {
15            System.out.println(fruit);
16        }
17    }
18}

Key Differences

The table below summarizes key differences between Collection and List:

FeatureCollectionList
Order and Positional AccessNo positional access, no ordering guaranteedOrdered collection with positional access
DuplicatesDepends on the implementing class (e.g., Set does not allow)Allows duplicates
Methods SupportedBasic methods like add(), remove()Additional methods like get(), set(), remove(int index)
Common ImplementationsHashSet, TreeSet, LinkedHashSetArrayList, LinkedList, Vector
Use CasesGeneral-purpose, non-sequential storageOrdered collections, frequent access by position

Additional Details

Performance Considerations

  • ArrayList vs LinkedList: When choosing between ArrayList and LinkedList for lists, consider that ArrayList provides faster random access due to underlying array implementation, while LinkedList offers faster insertions and deletions.

Interface Hierarchy

The java.util package outlines the hierarchy:

 
1Interface Collection<E>
2   ↳ Interface List<E>
3   ↳ Interface Set<E>
4   ↳ ...

Conclusion

While Collection is a broad framework serving as the backbone for other collection types, List offers more specialized functionality suited for scenarios where order is important and duplicates are permitted. Understanding the nuances between these interfaces helps developers choose the appropriate data structure while benefiting from Java's robust and flexible Collections Framework.


Course illustration
Course illustration

All Rights Reserved.