Introduction
The List.of
method, introduced in Java 9, provides a convenient way to create immutable lists. This method belongs to the java.util.List
interface and allows you to create a list with a fixed set of elements. The lists created using List.of
are immutable, meaning that their elements cannot be changed, added, or removed once the list is created.
Key Points:
- Immutability: The list created using
List.of
is immutable. - Convenience: Provides a simple and concise way to create lists with a fixed set of elements.
- Null Elements Not Allowed: The list cannot contain
null
elements.
Table of Contents
- Creating Lists with
List.of
- Characteristics of
List.of
- Common Use Cases
- Examples
- Conclusion
1. Creating Lists with List.of
The List.of
method provides several overloads to create lists with different numbers of elements.
Example:
import java.util.List;
public class ListOfExample {
public static void main(String[] args) {
// Creating a list with no elements
List<String> emptyList = List.of();
System.out.println("Empty List: " + emptyList);
// Creating a list with one element
List<String> singleElementList = List.of("one");
System.out.println("Single Element List: " + singleElementList);
// Creating a list with multiple elements
List<String> multipleElementsList = List.of("one", "two", "three");
System.out.println("Multiple Elements List: " + multipleElementsList);
}
}
Explanation:
- List.of(): Creates an empty list.
- List.of("one"): Creates a list with a single element.
- List.of("one", "two", "three"): Creates a list with multiple elements.
2. Characteristics of List.of
Lists created using List.of
have the following characteristics:
- Immutable: The list cannot be modified (i.e., no elements can be added, removed, or changed).
- Null Elements Not Allowed: Attempting to create a list with
null
elements will throw aNullPointerException
. - Fixed Size: The size of the list is fixed at the time of creation.
Example:
import java.util.List;
public class ListCharacteristicsExample {
public static void main(String[] args) {
List<String> list = List.of("one", "two", "three");
// Attempting to modify the list will throw UnsupportedOperationException
try {
list.add("four");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot add elements to the list: " + e);
}
try {
list.set(0, "zero");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify elements in the list: " + e);
}
try {
List.of("one", null);
} catch (NullPointerException e) {
System.out.println("Cannot create a list with null elements: " + e);
}
}
}
Explanation:
- list.add("four"): Attempting to add an element to the list throws an
UnsupportedOperationException
. - list.set(0, "zero"): Attempting to modify an element in the list throws an
UnsupportedOperationException
. - List.of("one", null): Attempting to create a list with
null
elements throws aNullPointerException
.
3. Common Use Cases
Use Case 1: Creating a Read-Only List
When you need a list that should not be modified, List.of
is an excellent choice.
Example:
import java.util.List;
public class ReadOnlyListExample {
public static void main(String[] args) {
List<String> readOnlyList = List.of("apple", "banana", "cherry");
System.out.println("Read-Only List: " + readOnlyList);
}
}
Use Case 2: Providing Fixed Data for Testing
When writing tests, you might need a list with a fixed set of elements.
Example:
import java.util.List;
public class FixedDataListExample {
public static void main(String[] args) {
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
System.out.println("Fixed Data List: " + numbers);
}
}
4. Examples
Example 1: Creating a List of Integers
import java.util.List;
public class IntegerListExample {
public static void main(String[] args) {
List<Integer> integers = List.of(1, 2, 3, 4, 5);
System.out.println("List of Integers: " + integers);
}
}
Example 2: Creating a List of Custom Objects
import java.util.List;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class CustomObjectListExample {
public static void main(String[] args) {
List<Person> people = List.of(new Person("Alice"), new Person("Bob"), new Person("Charlie"));
System.out.println("List of People: " + people);
}
}
Example 3: Attempting to Modify the List
import java.util.List;
public class ModifyListExample {
public static void main(String[] args) {
List<String> immutableList = List.of("a", "b", "c");
// Trying to add an element
try {
immutableList.add("d");
} catch (UnsupportedOperationException e) {
System.out.println("Add operation not supported: " + e);
}
// Trying to remove an element
try {
immutableList.remove("a");
} catch (UnsupportedOperationException e) {
System.out.println("Remove operation not supported: " + e);
}
// Trying to modify an element
try {
immutableList.set(0, "z");
} catch (UnsupportedOperationException e) {
System.out.println("Set operation not supported: " + e);
}
}
}
Example 4: Null Elements Not Allowed
import java.util.List;
public class NullElementListExample {
public static void main(String[] args) {
try {
List<String> listWithNull = List.of("a", null, "c");
} catch (NullPointerException e) {
System.out.println("Null elements not allowed: " + e);
}
}
}
5. Conclusion
The List.of
method provides a convenient way to create immutable lists in Java. It offers a simple and concise syntax for creating lists with a fixed set of elements. By understanding the characteristics and use cases of List.of
, you can effectively utilize this method to create read-only lists, provide fixed data for testing, and improve the maintainability of your code.