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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.studyeasy; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { LinkedList list1 = new LinkedList<>(); list1.add("Chaand"); list1.add("Jai"); list1.add("Veeru"); ArrayList list2 = new ArrayList<>(); list2.add("StudyEasy"); list2.add("organization"); list2.add("team"); new Main().printList(list1); new Main().printList(list2); } void printList(List list){ for (String name : list) { System.out.println(name); } } } |
Step-by-Step Code Explanation
- Imports:
ArrayList
,LinkedList
, andList
fromjava.util
are imported.
- Main Class:
- The
Main
class is the entry point of the program. Within themain
method, two types of lists are created: anArrayList
and aLinkedList
.
- The
- List Creation:
- The
list1
is created as aLinkedList
, holding names like “Chaand”, “Jai”, and “Veeru”. - The
list2
is created as anArrayList
, holding strings such as “StudyEasy”, “organization”, and “team”.
- The
- printList Method:
- The
printList
method accepts a parameter of typeList<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
andArrayList
due to the List interface’s common abstraction.
- The
Output
1 2 3 4 5 6 |
Chaand Jai Veeru StudyEasy organization team |
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.