Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

New Immutable Collections In Java 9

Java 9 introduced new factory methods to create immutable collections, such as List, Set, and Map. Immutable collections are collections that cannot be modified once they are created. They provide better performance and safety than their mutable counterparts because they prevent accidental modifications and reduce the overhead of defensive copying. In this tutorial, we'll discuss the new factory methods for creating immutable collections in Java 9 and provide examples of their usage.

  • Creating Immutable Lists

In Java 9, you can create an immutable list using the List.of() factory method. This method takes a variable number of elements and returns an unmodifiable list containing the specified elements.

Example:

import java.util.List;

public class ImmutableListExample {
    public static void main(String[] args) {
        List<String> names = List.of("Alice", "Bob", "Charlie");

        System.out.println("Immutable List: " + names);

        // The following line will throw an UnsupportedOperationException
        // because the list is immutable.
        // names.add("David");
    }
}

In this example, we create an immutable list of names using the List.of() method.

  • Creating Immutable Sets

Similar to lists, you can create an immutable set using the Set.of() factory method. This method takes a variable number of elements and returns an unmodifiable set containing the specified elements.

Example:

import java.util.Set;

public class ImmutableSetExample {
    public static void main(String[] args) {
        Set<Integer> numbers = Set.of(1, 2, 3, 4, 5);

        System.out.println("Immutable Set: " + numbers);

        // The following line will throw an UnsupportedOperationException
        // because the set is immutable.
        // numbers.add(6);
    }
}

In this example, we create an immutable set of numbers using the Set.of() method.

  • Creating Immutable Maps

To create an immutable map, use the Map.of() and Map.ofEntries() factory methods. The Map.of() method takes a variable number of key-value pairs and returns an unmodifiable map containing the specified entries. The Map.ofEntries() method takes a variable number of Map.Entry instances and returns an unmodifiable map containing the specified entries.

Example:

import java.util.Map;

public class ImmutableMapExample {
    public static void main(String[] args) {
        Map<String, Integer> scores = Map.of(
                "Alice", 90,
                "Bob", 85,
                "Charlie", 92
        );

        System.out.println("Immutable Map: " + scores);

        // The following line will throw an UnsupportedOperationException
        // because the map is immutable.
        // scores.put("David", 88);
    }
}

In this example, we create an immutable map of scores using the Map.of() method.

In conclusion, Java 9 introduced new factory methods for creating immutable collections, such as List, Set, and Map. Immutable collections provide better performance and safety than their mutable counterparts, as they prevent accidental modifications and reduce the overhead of defensive copying. Using the new factory methods in Java 9, you can easily create immutable collections that are both efficient and secure.

  1. Creating and using immutable collections in Java 9

    Java 9 introduced convenient methods for creating immutable collections:

    List<String> immutableList = List.of("Java", "Immutable", "Collections");
    Set<Integer> immutableSet = Set.of(1, 2, 3, 4, 5);
    Map<String, Integer> immutableMap = Map.of("One", 1, "Two", 2, "Three", 3);
    
  2. List.of(), Set.of(), and Map.of() in Java 9

    The List.of(), Set.of(), and Map.of() methods provide a concise way to create immutable collections:

    List<String> immutableList = List.of("Java", "Immutable", "Collections");
    Set<Integer> immutableSet = Set.of(1, 2, 3, 4, 5);
    Map<String, Integer> immutableMap = Map.of("One", 1, "Two", 2, "Three", 3);
    
  3. Java 9 enhancements for immutable data structures

    Java 9 introduced several enhancements, including the List.of(), Set.of(), and Map.of() methods, making it easier to create immutable collections.

  4. Working with unmodifiable collections in Java 9

    You can make existing collections unmodifiable using the Collections.unmodifiableXXX() methods:

    List<String> originalList = new ArrayList<>();
    List<String> unmodifiableList = Collections.unmodifiableList(originalList);
    
  5. Using copyOf() method for immutable collections in Java 9

    Java 9 introduced the copyOf() method to create immutable collections from existing collections:

    List<String> originalList = new ArrayList<>();
    List<String> immutableList = List.copyOf(originalList);
    
  6. Migration strategies to Java 9 immutable collections

    When migrating to Java 9, replace mutable collections with immutable equivalents:

    // Before
    List<String> mutableList = new ArrayList<>();
    
    // After (Java 9)
    List<String> immutableList = List.of("Java", "Immutable", "Collections");
    

    Update existing code to use the new methods and take advantage of the benefits of immutability.