How to use the Collectors.toUnmodifiableList() and other new Collectors in Java 10

In Java 10, a significant enhancement was introduced to the java.util.stream.Collectors class: new utility methods to create unmodifiable collections such as lists and sets. One notable method is Collectors.toUnmodifiableList(). This method allows you to efficiently create immutable lists during stream processing, adding to the immutability features provided by Java 9 and earlier versions.

Here’s how you can use Collectors.toUnmodifiableList() and other similar methods introduced in Java 10:


1. Using Collectors.toUnmodifiableList()

The Collectors.toUnmodifiableList() collector creates an unmodifiable list from a stream of elements. This means the resulting list cannot be modified (no adding, removing, or updating elements). If you attempt to modify it, a runtime exception (UnsupportedOperationException) will be thrown.

Example:

package org.kodejava.util.stream;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UnmodifiableList {
    public static void main(String[] args) {
        // Example list using Collectors.toUnmodifiableList
        List<String> unmodifiableList = Stream.of("A", "B", "C")
                .collect(Collectors.toUnmodifiableList());

        System.out.println("Unmodifiable List: " + unmodifiableList);

        // Attempt to modify the list will throw UnsupportedOperationException
        unmodifiableList.add("D"); // This will throw a runtime exception!
    }
}

Output:

Unmodifiable List: [A, B, C]
Exception in thread "main" java.lang.UnsupportedOperationException

2. Other Collectors Introduced in Java 10

Java 10 introduced two other collectors similar to toUnmodifiableList():

  • Collectors.toUnmodifiableSet()
    • Creates an unmodifiable set from a stream of elements.
    • Duplicate elements will be removed since it’s a set.

Example:

package org.kodejava.util.stream;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UnmodifiableSet {
    public static void main(String[] args) {
        Set<String> unmodifiableSet = Stream.of("A", "B", "C", "A") // "A" will appear only once
                .collect(Collectors.toUnmodifiableSet());
        System.out.println("Unmodifiable Set: " + unmodifiableSet);

        unmodifiableSet.add("D"); // Throws UnsupportedOperationException
    }
}
  • Collectors.toUnmodifiableMap()
    • Creates an unmodifiable map using key-value pairs from a stream.
    • Requires a way to specify the key and value in the collector.
    • If duplicate keys are produced, it will throw an IllegalStateException.

Example:

package org.kodejava.util.stream;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UnmodifiableMap {
    public static void main(String[] args) {
        Map<Integer, String> unmodifiableMap = Stream.of("A", "B", "C")
                .collect(Collectors.toUnmodifiableMap(
                        String::length,  // Key mapper
                        v -> v           // Value mapper
                ));

        System.out.println("Unmodifiable Map: " + unmodifiableMap);

        // Attempting to modify will throw an UnsupportedOperationException
        unmodifiableMap.put(2, "D"); // Throws UnsupportedOperationException
    }
}

3. Behavior of Unmodifiable Collections

  • These collectors guarantee that:
    • The collection cannot be modified (no add, remove, put, etc.).
    • Any attempt to modify them results in an UnsupportedOperationException.
    • They are safe to use for read-only purposes.
  • If the stream itself contains null values, a NullPointerException will be thrown.

4. Best Uses of toUnmodifiable*() Collectors

  • Ensuring immutability for collections to prevent accidental modifications.
  • Useful in multi-threaded or concurrent applications where immutability eliminates thread-safety issues.
  • Perfect for cases where only read access is required.

5. Comparison with Java 9 List.of()

Java 9 introduced factory methods like List.of(), Set.of(), and Map.of() for creating immutable collections. While those methods are concise, the new collectors offer more flexibility when working with streams.

Java 9 Example:

List<String> immutableList = List.of("A", "B", "C");

Java 10 Stream Example:

List<String> immutableList = Stream.of("A", "B", "C")
                                   .collect(Collectors.toUnmodifiableList());

Summary Table:

Collector Description Introduced in
Collectors.toUnmodifiableList() Creates an unmodifiable List Java 10
Collectors.toUnmodifiableSet() Creates an unmodifiable Set Java 10
Collectors.toUnmodifiableMap() Creates an unmodifiable Map Java 10

Conclusion

The Collectors.toUnmodifiableList() and related methods introduced in Java 10 are powerful tools for creating immutable collections directly from streams. They ensure immutability, improve code safety, and fit well into functional programming paradigms introduced with Java Streams.

How do I add an object to the beginning of Stream?

To add an object to the beginning of a list using Java Streams, we typically cannot directly prepend an object in a stream-friendly way because Streams themselves are immutable and don’t directly modify the original collection. However, we can achieve this by creating a new list with the desired order.

Here’s how we can approach it:

package org.kodejava.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamBeginningAdd {
    public static void main(String[] args) {
        List<String> originalList = Arrays.asList("B", "C", "D");
        String newElement = "A";

        // Add the new element at the beginning using Stream
        List<String> updatedList = Stream.concat(Stream.of(newElement), originalList.stream())
                .collect(Collectors.toList());

        // Output: [A, B, C, D]
        System.out.println(updatedList);
    }
}

Explanation:

  1. Stream.of(newElement): Wraps the new element as a single-element stream.
  2. originalList.stream(): Converts the existing list into a stream.
  3. Stream.concat(): Combines the two streams — placing the newElement stream first and the original list’s stream second.
  4. collect(Collectors.toList()): Materializes (collects) the combined stream into a new list.

This ensures immutability of the original list and creates a new list with the desired order.

How do I sum a BigDecimal property of a list of objects using Java Stream API?

If we have a list of objects, and we want to sum a BigDecimal property of these objects, we can achieve this using the Java Stream API. This API provides a clean and efficient way to process collections of objects. To sum the BigDecimal amounts, you can use the map and reduce methods of the Stream API.

As an example, we have a class named Transaction with a BigDecimal property named amount. We have a list of Transaction objects, and we want to calculate the total sum of the amount properties.

In the code snippet below we do the following:

  • Creating Transactions: We create a list of Transaction objects, each with a different BigDecimal amount.
  • Filter Transactions and its amount: We filter to exclude the null transaction and null transaction amount.
  • Mapping to Amounts: We use the map method to convert each Transaction object to its amount property.
  • Summing the Amounts: The reduce method takes two parameters: an identity value (BigDecimal.ZERO) and an accumulator function (BigDecimal::add). The accumulator function adds each BigDecimal in the stream to the running total.
  • Printing the Result: Finally, we print the total sum of the amounts.
package org.kodejava.stream;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class BigDecimalSumExample {
    public static void main(String[] args) {
        // Create a list of transaction objects
        List<Transaction> transactions = Arrays.asList(
                new Transaction(new BigDecimal("10.50")),
                null,
                new Transaction(new BigDecimal("30.25")),
                new Transaction(null),
                new Transaction(new BigDecimal("11.49"))
        );

        // Sum the amount properties using stream
        BigDecimal totalAmount = transactions.stream()
                // Filter out null Transaction objects and Transaction objects
                // with null amounts
                .filter(t -> t != null && t.getAmount() != null)
                .map(Transaction::getAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // Print the result
        System.out.println("Total Amount: " + totalAmount);
    }

    static class Transaction {
        private final BigDecimal amount;

        public Transaction(BigDecimal amount) {
            this.amount = amount;
        }

        public BigDecimal getAmount() {
            return amount;
        }
    }
}

Below is another example, we want to sum just a List<BigDecimal> values. To sum the values we can use the reduce method as shown in the code snippet below.

package org.kodejava.stream;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class BigDecimalListSumExample {
    public static void main(String[] args) {
        // Create a list of BigDecimal values
        List<BigDecimal> amounts = Arrays.asList(
                new BigDecimal("10.50"),
                new BigDecimal("20.75"),
                new BigDecimal("30.25"),
                null,
                new BigDecimal("11.49")
        );

        // Sum the BigDecimal values using stream
        BigDecimal totalAmount = amounts.stream()
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // Print the result
        System.out.println("Total Amount: " + totalAmount);
    }
}

Using Java Stream API to sum a BigDecimal property of a list of objects or a list of BigDecimal values are both concise and efficient. The map and reduce methods streamline the process, making our code more readable and maintainable. This approach can be applied to various scenarios where we need to aggregate data from a list of objects.

How do I use Collectors.maxBy() method?

The Collectors.maxBy() method is used to find the maximum element from a stream based on a certain comparator. It returns an Optional which contains the maximum element according to the provided comparator, or an empty Optional if there are no elements in the stream.

Here’s a simple example where we have a list of integers, and we want to find the biggest integer:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class MaxByDemo {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> maxNumber = numbers.stream()
                .collect(Collectors.maxBy(Comparator.naturalOrder()));

        maxNumber.ifPresent(System.out::println);
    }
}

In this example:

  • We create a Stream from the list of integers.
  • We then use Collectors.maxBy(Comparator.naturalOrder()) to get the maximum number. Comparator.naturalOrder() is a shortcut for Comparator.comparing(Function.identity()).
  • Collectors.maxBy() returns an Optional because the stream could be empty.
  • We print the maximum number if it exists.

When you run this program, it will print “5” because 5 is the biggest number in the list.

Keep in mind that if the stream is empty, maxNumber will be an empty Optional, and nothing will be printed.

How do I use Collectors.minBy() method?

The Collectors.minBy() method in Java 8 is used to find the minimum element from a stream of elements based on a certain comparator. It returns an Optional describing the minimum element of the stream, or an empty Optional if the stream is empty.

Here’s an example of how to use Collectors.minBy(). Assume we have a list of integers, and we want to find the smallest element.

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class CollectorsMinBy {
    public static void main(String... args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> min = numbers.stream()
                .collect(Collectors.minBy(Integer::compare));

        min.ifPresent(System.out::println);
    }
}

In this code:

  • We have a list of integers.
  • We create a Stream from the list and collect the stream into an Optional that might hold the minimum value via the Collectors.minBy(Integer::compare) collector.
  • Integer::compare is a method reference that is used to instruct Collectors.minBy() on how to compare the integers.
  • min.ifPresent(System.out::println) checks if the Optional has a value. If it does, the value is passed to the System.out::println method and printed to the console.

When run, this program prints the smallest number in our list, which is “1”.

Note that if the list is empty, min will hold an empty Optional, and min.ifPresent(System.out::println) will not print anything.

Here’s another example of how you can use the Collectors.minBy() method to find the object containing the minimum value for a certain property. Let’s assume we have a Person class and a list of Person objects, and we want to find which Person has the smallest age.

package org.kodejava.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class CollectorsMinByObjectProperty {
    public static void main(String... args) {
        List<Person> people = Arrays.asList(
                new Person("Rosa", 21),
                new Person("Bob", 25),
                new Person("Alice", 18),
                new Person("John", 22));

        Optional<Person> youngestPerson = people.stream()
                .collect(Collectors.minBy(Comparator.comparingInt(Person::getAge)));

        youngestPerson.ifPresent(System.out::println);
    }

    static class Person {
        String name;
        int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "Person{" +
                   "name='" + name + '\'' +
                   ", age=" + age +
                   '}';
        }
    }
}

Output:

Person{name='Alice', age=18}

In this code:

  • The Person class has two fields, name and age, and a getter for the age field.
  • We have a list of Person objects.
  • We create a Stream from the list and then use Collectors.minBy() to find the Person with the smallest age. To do this, we use Comparator.comparingInt(Person::getAge), which compares the Person objects based on their age.
  • Collectors.minBy() returns an Optional that might hold the Person with the smallest age.
  • If such a Person exists, we print that Person using System.out::println.

This program prints: Person{name='Alice', age=18}, as Alice is the person with the smallest age.