Introduction
In Java, a List
is an ordered collection that allows for duplicate elements. It provides methods to manipulate the sequence of its elements. This tutorial will guide you through various ways to create and manage lists in Java, utilizing different versions and libraries.
Creating Lists in Java
Using ArrayList
The most common implementation of the List
interface is ArrayList
. Here’s how you can create an ArrayList
:
// Without generics (not recommended)
List myList = new ArrayList();
// With generics (Java 7 and later, using diamond operator)
List<String> myList = new ArrayList<>();
// With generics (Old Java versions)
List<String> myList = new ArrayList<String>();
Using Arrays.asList
For a fixed-size list that cannot be modified after creation:
List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
This method is useful for creating immutable lists from arrays or collections.
Java 8 and Later Enhancements
Creating Lists in Java 8
Java 8 introduced lambda expressions and functional interfaces, but for list creation:
// Fixed-size list (immutable)
List<Integer> list = Arrays.asList(1, 2); // Note: list.set(...) is supported
// Non-empty mutable list
List<Integer> list = new ArrayList<>(Arrays.asList(3, 4));
Java 9 Improvements
Java 9 introduced factory methods for collections:
// Immutable list using List.of()
List<Integer> immutableList = List.of(1, 2);
// Creating a mutable list from an immutable one
List<Integer> mutableList = new ArrayList<>(List.of(3, 4));
Java 10: Local Variable Type Inference
Java 10 introduced the var
keyword for local variable type inference:
var list1 = List.of(1, 2); // Immutable list
var list2 = new ArrayList<>(List.of(3, 4)); // Mutable list from an immutable one
var list3 = new ArrayList<String>(); // Explicit type declaration with var
Using Third-Party Libraries: Guava
Google’s Guava library provides additional utilities for creating lists:
// Creating a mutable list
List<String> names = Lists.newArrayList("Mike", "John", "Lesly");
// Immutable list using Guava
ImmutableList<String> immutableNames = ImmutableList.of("A", "B");
Specialized List Creations with Guava
-
Characters from a String:
List<Character> characters = Lists.charactersOf("String");
-
Integers List:
List<Integer> numbers = Ints.asList(1, 2, 3);
Best Practices in Java
Avoid Raw Types
Since Java 5, generics have been a part of the language. Always use them to ensure type safety:
// Good practice
List<String> list = new ArrayList<>();
// Bad practice
List list = new ArrayList(); // Use raw types only when necessary and unavoidable
Program to Interfaces
Always program to interfaces rather than implementations for flexibility:
// Good practice
List<Double> list = new ArrayList<>();
// Bad practice
ArrayList<Double> list = new ArrayList<>(); // Avoid tying your code to a specific implementation
Immutable and Empty Lists
Creating immutable lists ensures that the list cannot be modified after creation, which is useful for maintaining data integrity:
// Using Collections
List<String> unmodifiableList = Collections.unmodifiableList(new ArrayList<>(Arrays.asList("A", "B")));
// Using Guava
ImmutableList<String> guavaList = ImmutableList.of("A", "B");
// Creating empty immutable lists
List<String> emptyList = Collections.emptyList();
Conclusion
Understanding how to create and manage lists in Java is fundamental for efficient data handling. By leveraging the capabilities of ArrayList
, factory methods, and libraries like Guava, you can write robust and flexible code. Remember to follow best practices such as using generics and programming to interfaces to enhance your Java applications.