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 avoid Optional as method parameter and why it matters?

Using Optional as a method parameter in Java is discouraged because it goes against the intended purpose of Optional and can lead to inefficiencies, poor readability, and unintended complications in the code. Here’s why it matters and how to avoid using Optional as a method parameter.


Why Should You Avoid Optional as a Method Parameter?

  1. Misuse of Optional‘s Purpose:
    • Optional was designed as a return type to explicitly signal that a value could either be present or absent (to avoid null and NullPointerException issues).
    • Passing Optional as a parameter suggests that the caller has to wrap arguments in Optional, which adds unnecessary complexity and overhead.
  2. Reduces Code Readability:
    • Method signatures become harder to read and understand when parameters are wrapped in Optional. It may confuse collaborators who aren’t expecting this pattern.
  3. Boilerplate Code for Callers:
    • Callers would have to wrap or handle Optional arguments before invoking the method, which adds clunky and cumbersome boilerplate code.
    • Example: myMethod(Optional.of(value)); is less intuitive compared to myMethod(value);.
  4. Performance Overhead:
    • Using Optional as a parameter adds unnecessary memory usage because it needs to instantiate an Optional wrapper, which could be avoided altogether.
  5. Violates Principle of Responsibility:
    • The responsibility for checking the validity or presence of a value should remain inside the method, not outside it. The caller shouldn’t decide how to build the Optional.

What to Do Instead?

  1. Use Null or Overloaded Methods:
    • If a parameter is optional, you can use method overloading or make it null-safe with a clear explanation in the documentation.
    public void myMethod(String optionalValue) {
       if (optionalValue != null) {
           // Process the value
       }
    }
    
    // Overloaded method
    public void myMethod() {
       myMethod(null);
    }
    
  2. Provide Default Values:
    • If you anticipate optional behavior, provide a default value instead of Optional.
    public void myMethod(String value) {
       // Use a default value if it's null
       String processedValue = value != null ? value : "default";
       // Process
    }
    
  3. Caller-Side Null Check:
    • Let the caller handle whether they pass null, while ensuring your method handles it gracefully.
  4. Null-Object Pattern:
    • Instead of using Optional, use a well-defined null-object pattern or sentinel values.

Why This Matters?

  1. Cleaner APIs:
    • Avoiding Optional parameters results in cleaner, more maintainable, and understandable APIs.
  2. Encapsulation and Responsibility:
    • The responsibility of deciding whether a parameter is present should belong inside the method. This encapsulation aligns with good design principles.
  3. Interoperability:
    • Most developers are familiar with methods that accept parameters directly or allow null. Using Optional for parameters deviates from common practices, making it harder to integrate with or extend the project.
  4. Readability and Maintainability:
    • Code is easier to reason about when method signatures are straightforward, without unnecessary abstraction layers like wrapping parameters in Optional.

Example Comparison

BAD: Using Optional as a Parameter

public void processData(Optional<String> data) {
    if (data.isPresent()) {
        System.out.println(data.get());
    } else {
        System.out.println("No data");
    }
}

// Caller
processData(Optional.of("value"));
processData(Optional.empty());

Issues:

  • Boilerplate for callers (Optional.of or Optional.empty).
  • Misuse of the Optional class.
  • Code feels clunky and counterintuitive.

GOOD: Without Optional as a Parameter

public void processData(String data) {
    if (data != null) {
        System.out.println(data);
    } else {
        System.out.println("No data");
    }
}

// Caller
processData("value");
processData(null);

Solution:

  • Cleaner and more straightforward for both the method’s implementation and the caller.

Conclusion

To avoid potential pitfalls, reserve Optional for return types (to express optionality in results of computations) and never use it in method parameters. This ensures better code readability, proper encapsulation of logic, and a cleaner API design.

How do I integrate Optional with Java Streams?

Integrating Optional with Java Streams can simplify many common scenarios when working with potentially absent values. Here are different techniques depending on your specific use case:

1. Use Optional in Stream Pipelines

When you have an Optional and you want to integrate it into a Stream pipeline, you can use stream() from Java 9 onward. The stream() method will return a single-element stream if a value is present, or an empty stream otherwise.

Example:

package org.kodejava.util;

import java.util.Optional;
import java.util.stream.Stream;

public class OptionalWithStream {
    public static void main(String[] args) {
        Optional<String> optionalValue = Optional.of("Hello, Stream!");

        // Convert Optional to a Stream and process it
        optionalValue.stream()
                .map(String::toUpperCase)
                .forEach(System.out::println);
    }
}

Output:

HELLO, STREAM!

2. Use Streams to Produce Optionals

Stream operations often result in an Optional, such as methods like findFirst(), findAny(), and max().

Example:

package org.kodejava.util;

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

public class StreamToOptional {
    public static void main(String[] args) {
        List<String> values = Arrays.asList("a", "b", "c", "d");

        // Find the first value that matches a condition
        Optional<String> result = values.stream()
                .filter(value -> value.equals("b"))
                .findFirst();

        result.ifPresent(System.out::println); // Output: b
    }
}

3. Flatten Optional<Optional<T>> in Stream Pipelines

If you end up with a nested Optional<Optional<T>>, you can use flatMap() to flatten it.

Example:

package org.kodejava.util;

import java.util.Optional;

public class NestedOptional {
    public static void main(String[] args) {
        Optional<Optional<String>> nestedOptional = Optional.of(Optional.of("Value"));

        // Flatten the nested Optional
        nestedOptional.flatMap(inner -> inner)
                .ifPresent(System.out::println); // Output: Value
    }
}

Similarly, if you’re working with streams, you can achieve something equivalent:

package org.kodejava.util;

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

public class OptionalWithStream {
    public static void main(String[] args) {
        List<Optional<String>> optionals = List.of(Optional.of("A"), Optional.empty(), Optional.of("B"));

        // Flatten the optional values into a single stream
        List<String> results = optionals.stream()
                .flatMap(Optional::stream)
                .collect(Collectors.toList());

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

4. Filter Optional Using Stream

If you want to filter the Optional based on some condition before further processing, using filter() is concise and effective.

Example:

package org.kodejava.util;

import java.util.Optional;

public class FilterOptionalWithStream {
    public static void main(String[] args) {
        Optional<String> optional = Optional.of("hello");

        // Filter and process the value if it passes the condition
        optional.filter(value -> value.length() > 4)
                .ifPresent(System.out::println); // Output: hello
    }
}

5. Handle Streams with Empty Optionals

If you have a situation where an Optional can be empty and you want to safely handle values, you can convert the Optional into a Stream and continue processing.

Example:

package org.kodejava.util;

import java.util.Optional;
import java.util.stream.Stream;

public class EmptyOptionalStream {
    public static void main(String[] args) {
        Optional<String> optional = Optional.empty();

        optional.stream()
                .map(String::toUpperCase)
                .forEach(System.out::println);
        // No output, as the Optional is empty
    }
}

6. Combine Optional and Stream Elements

You can also work with a mix of Stream elements and Optionals. This is especially useful for chaining or merging operations.

Example:

package org.kodejava.util;

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

public class CombineOptionalWithStream {
    public static void main(String[] args) {
        List<String> list = List.of("foo", "bar");
        Optional<String> optionalValue = Optional.of("baz");

        Stream<String> combinedStream = Stream.concat(list.stream(), optionalValue.stream());

        // Output: foo, bar, baz
        combinedStream.forEach(System.out::println);
    }
}

Summary of Key Methods:

  • Convert Optional to Stream: Optional.stream() (Java 9+)
  • Flatten nested Optionals: flatMap(Optional::stream)
  • Handle presence or absence: filter() or orElse()/orElseGet()
  • Produce Optionals from Streams: Use stream terminal operations like findFirst(), findAny(), max(), and min()
  • Combine Streams and Optionals: Leverage Stream.concat() or Optional.stream()

By effectively combining Optional and Stream, you can avoid null checks and achieve a functional, clean approach to processing sequences in Java.

How to create cleaner code with type inference in Java 10

Type inference was introduced in Java 10 with the new var keyword, enabling developers to declare local variables without explicitly specifying their type. This feature can help create cleaner, more concise code by reducing boilerplate, though it should be used judiciously to maintain code readability.

Here’s a guide on how to use type inference effectively and write cleaner code in Java 10 and later:


1. Use var for Local Variables

The var keyword allows you to declare local variables without explicitly stating their type. The compiler infers the type based on the expression assigned to the variable. Here’s how it works:

Example:

var message = "Hello, World!"; // Compiler infers this as String
var count = 42;                // Compiler infers this as int
var list = new ArrayList<String>(); // Compiler infers this as ArrayList<String>

System.out.println(message);  // Hello, World!
System.out.println(count);    // 42

Benefits:

  • Eliminates redundancy. For instance:
List<String> list = new ArrayList<>();

becomes:

var list = new ArrayList<String>();

2. Use var in Loops

In for-each loops and traditional for-loops, var can simplify the code:

Example:

var numbers = List.of(1, 2, 3, 4, 5);
for (var num : numbers) {
    System.out.println(num); // Iterates through the numbers
}

Benefits:

  • Avoids unnecessary type declarations while maintaining readability.

3. Use var with Streams and Lambdas

var integrates well with Java Streams and Lambda expressions to reduce verbosity:

Example:

var numbers = List.of(1, 2, 3, 4, 5);
var result = numbers.stream()
                    .filter(n -> n % 2 == 0)
                    .map(n -> n * 2)
                    .toList();

System.out.println(result); // [4, 8]

When working with complex streams, var can make code shorter and easier to follow.


4. Restrictions on var

While var is versatile, there are some limitations and rules:

  • Only for Local Variables: var can only be used for local variables, loop variables, and indexes, not for class fields, method parameters, or return types.
  • Compiler Must Infer Type: You must assign a value to a var. For example, the following won’t work:
var uninitialized; // Error: cannot use 'var' without initializer
  • Anonymous Classes: Avoid overuse with anonymous classes to maintain clarity.

5. Maintain Readability

While var can simplify code, readability should always be a priority. Overusing var can obscure the code’s intent, especially when dealing with complex types:

Example of Overuse:

var map = new HashMap<List<String>, Set<Integer>>(); // Hard to understand

In such cases, it’s better to use explicit types.


6. Good Practices

  • Use var for Obvious Types:
var name = "John Doe"; // Obviously String
  • Avoid var for Ambiguous Types:
// Original:
var data = performOperation(); // What is the return type?
// Better:
List<String> data = performOperation();
  • Avoid Excessive Chaining:

    Using var with complex chains can make debugging harder. Be explicit when needed.


7. Refactoring Example

Here’s how you can refactor code for better clarity using var:

Before Refactoring:

ArrayList<String> names = new ArrayList<>();
HashMap<String, Integer> nameAgeMap = new HashMap<>();

After Refactoring:

var names = new ArrayList<String>();
var nameAgeMap = new HashMap<String, Integer>();

This is concise without sacrificing clarity.


Conclusion

Type inference with var in Java 10 improves code conciseness and readability when used appropriately. To ensure cleaner code:

  • Use var for obvious and readable scenarios.
  • Avoid using var when the inferred type is unclear or ambiguous.
  • Focus on balancing conciseness with the need for maintainable and self-explanatory code.

How do I combine multiple Optionals in functional-style code?

Combining multiple Optional objects in Java in a functional style is a common need, especially when working with potentially nullable values without resorting to null checks. Here are examples of some approaches you can use based on the scenario:


1. Combining If All Optionals Are Present

If you want to combine values only when all Optionals are non-empty, you can use flatMap() and map() to transform and combine their values.

Example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalCombination {
    public static void main(String[] args) {
        Optional<String> optional1 = Optional.of("Hello");
        Optional<String> optional2 = Optional.of("World");

        Optional<String> combined = optional1.flatMap(val1 ->
                optional2.map(val2 -> val1 + " " + val2)
        );

        // Output: Hello World
        combined.ifPresent(System.out::println); 
    }
}

Here:

  • flatMap is used on the first Optional.
  • map is applied on the second Optional inside the flatMap block.
  • This ensures the operation occurs only if both Optionals are present.

2. Using Multiple Optionals Dynamically with Streams

If you have multiple Optional objects, a dynamic approach using streams may be more suitable.

Example:

package org.kodejava.util;

import java.util.Optional;
import java.util.stream.Stream;

public class OptionalCombinationWithStreams {
    public static void main(String[] args) {
        Optional<String> optional1 = Optional.of("Hello");
        Optional<String> optional2 = Optional.of("Functional");
        Optional<String> optional3 = Optional.of("Java");

        String result = Stream.of(optional1, optional2, optional3)
                .flatMap(Optional::stream)
                .reduce((s1, s2) -> s1 + " " + s2)
                .orElse("No values");

        // Output: Hello Functional Java
        System.out.println(result);
    }
}

Steps in this approach:

  1. Use Stream.of() to collect your Optional objects.
  2. Extract their values using flatMap(Optional::stream).
  3. Combine the values with reduce.

3. Getting the First Non-Empty Optional

Sometimes, you’re only interested in the first non-empty Optional. For this, you can use Optional.or(), which was introduced in Java 9.

Example:

package org.kodejava.util;

import java.util.Optional;

public class FirstNonEmptyOptional {
    public static void main(String[] args) {
        Optional<String> optional1 = Optional.empty();
        Optional<String> optional2 = Optional.of("Hello");
        Optional<String> optional3 = Optional.empty();

        Optional<String> firstPresent = optional1
                .or(() -> optional2)
                .or(() -> optional3);

        // Output: Hello
        firstPresent.ifPresent(System.out::println);
    }
}

4. Handling Custom Logic with Optionals

You can define custom logic to process multiple Optionals and combine them using a utility function when needed.

Example:

package org.kodejava.util;

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

public class OptionalCustomCombination {
    public static void main(String[] args) {
        Optional<Integer> optional1 = Optional.of(10);
        Optional<Integer> optional2 = Optional.of(20);
        Optional<Integer> optional3 = Optional.empty();

        Optional<Integer> combined = combineOptionals(optional1, optional2, optional3);
        combined.ifPresent(System.out::println); // Output: 30
    }

    @SafeVarargs
    public static Optional<Integer> combineOptionals(Optional<Integer>... optionals) {
        return Stream.of(optionals)
                .flatMap(Optional::stream)
                .collect(Collectors.reducing(Integer::sum));
    }
}

In this example:

  • The combineOptionals method dynamically handles any number of Optional<Integer>.
  • Non-empty values are summed using Collectors.reducing().

Which Pattern Should You Use?

  • Combine Only When All Optionals Are Present: Use flatMap and map chaining.
  • Combine Dynamically with Multiple Optionals: Use a Stream.
  • Use First Non-Empty Optional: Use Optional.or().
  • Custom Processing Logic: Create a reusable utility method.

This way, you can handle Optional objects cleanly and avoid verbose null checks.