S09L11 – List interface in Java Collections framework

List Interface in Java Collections Framework

Table of Contents:

1. Introduction

In this chapter, we explore the List interface in Java, which is part of the Collections framework.
Java developers frequently encounter the need to work with lists, be it for managing records, holding data, or manipulating objects.
The List interface serves as the backbone for various implementations like ArrayList and LinkedList,
offering flexibility and consistency in data handling.

Understanding how the List interface works, and how it can be utilized with different implementations,
is essential for effective Java programming, especially in the context of collection handling.

2. The List Interface

2.1 Overview

The List interface in Java is part of the java.util package and extends the Collection interface.
It represents an ordered collection (also known as a sequence). Lists allow precise control over where each element is inserted.
A user can access elements by their integer index and search for elements in the list.

2.2 Key Features of List Interface

  • Duplicates allowed: A list can contain duplicate elements.
  • Order of insertion: Elements in a list are maintained in the order they are inserted.
  • Index-based access: List elements can be accessed via their index.
  • Null elements: A list can contain null elements (though there might be exceptions based on the list type).

2.3 List vs ArrayList vs LinkedList

Feature ArrayList LinkedList
Data structure Resizable array Doubly linked list
Access time O(1) for random access (constant) O(n) for random access
Insertion time O(n) (depends on size and index) O(1) for add/remove at ends
Memory overhead Less overhead Higher overhead due to node storage

3. Working with List Interface in Java

Example Code

Step-by-Step Code Explanation

  • Imports:
    • ArrayList, LinkedList, and List from java.util are imported.
  • Main Class:
    • The Main class is the entry point of the program. Within the main method, two types of lists are created: an ArrayList and a LinkedList.
  • List Creation:
    • The list1 is created as a LinkedList, holding names like “Chaand”, “Jai”, and “Veeru”.
    • The list2 is created as an ArrayList, holding strings such as “StudyEasy”, “organization”, and “team”.
  • printList Method:
    • The printList method accepts a parameter of type List<String>, demonstrating the flexibility of the List interface.
    • The for-each loop iterates through the list, printing each name.
    • This method can handle both the LinkedList and ArrayList due to the List interface’s common abstraction.

Output

4. Conclusion

The List interface in Java provides a robust structure for handling ordered collections. Whether you choose ArrayList for fast access times or LinkedList for efficient insertions and deletions, the List interface ensures consistency in how you interact with these data structures. Leveraging the List interface allows for cleaner, more maintainable code, especially when working with multiple implementations like ArrayList and LinkedList.