How do I use Predicate.not() in Streams?

To use Predicate.not() in streams, you take advantage of its ability to negate an existing predicate. This can be helpful in filter operations where you want to filter out elements that match a given condition instead of including them.

Here’s how you can use Predicate.not in streams:

Basic Explanation

  1. What it does: The Predicate.not() method is a static method (added in Java 11) that creates a predicate that negates the specified predicate. Instead of writing complex logic for negation, you can directly use Predicate.not() for cleaner and more readable code.

  2. Use Case in Streams: When working with Java Streams, you often use .filter() to include elements that satisfy a condition. If you want to exclude elements that satisfy a condition, you can use Predicate.not().


Example: Using Predicate.not() in a Stream

package org.kodejava.util.function;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PredicateNotExample {
    public static void main(String[] args) {
        // Define a list of integers
        List<Integer> numbers = List.of(5, 15, 8, 25, 3, 12);

        // Define a predicate to filter numbers greater than 10
        Predicate<Integer> isGreaterThan10 = number -> number > 10;

        // Use Predicate.not() to filter numbers NOT greater than 10
        List<Integer> filteredNumbers = numbers.stream()
                .filter(Predicate.not(isGreaterThan10))
                .collect(Collectors.toList());

        // Print the filtered list
        System.out.println(filteredNumbers); // Output: [5, 8, 3]
    }
}

Breakdown of the Code:

  1. Define Predicate
    A predicate is defined (isGreaterThan10) to test if a number is greater than 10.

  2. Stream Filtering

    • Using .stream() to process the list of numbers.
    • .filter(Predicate.not(isGreaterThan10)) negates the predicate, effectively including numbers less than or equal to 10.
  3. Collect Results
    The result is collected using .collect(Collectors.toList()).


Why Use Predicate.not()?

  • Improved Readability: Instead of writing a negation explicitly like x -> !isGreaterThan10.test(x), you can use Predicate.not(isGreaterThan10) for better readability.

  • Reusability: Predicate.not() can work for any predicate, making it easier to reuse your existing predicates in multiple ways.

  • Less Prone to Errors: Writing custom negation logic in lambdas may lead to errors or make the code harder to understand. Predicate.not() makes intent clear and reduces the chance of mistakes.


Notes:

  • The Predicate.not() method was introduced in Java 11. Ensure you are using Java 11 or later to use it.
  • You can apply this with any kind of predicate—numerical, string-based, or custom objects.

How do I use Collectors::teeing in Streams?

In Java, Collectors::teeing is a feature introduced in Java 12 that allows you to collect elements of a stream using two different collectors and then combine the results using a BiFunction.

Syntax:

static <T, R1, R2, R> Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1,
                                                Collector<? super T, ?, R2> downstream2,
                                                BiFunction<? super R1, ? super R2, R> merger)

Concept:

  1. downstream1: The first collector for processing input elements.
  2. downstream2: The second collector for processing input elements.
  3. merger: A BiFunction that merges the results of the two collectors.

The teeing collector is useful when you want to process a stream in two different ways simultaneously and combine the results.


Example 1: Calculate the Average and Sum of a Stream

Here’s how you can calculate both the sum and average of a list of integers simultaneously:

package org.kodejava.util.stream;

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

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

        var result = numbers.stream()
                .collect(Collectors.teeing(
                        Collectors.summingInt(i -> i),             // Collector 1: Sum
                        Collectors.averagingInt(i -> i),           // Collector 2: Average
                        (sum, avg) -> "Sum: " + sum + ", Avg: " + avg // Merger
                ));

        System.out.println(result); // Output: Sum: 15, Avg: 3.0
    }
}

Example 2: Get Statistics (Min and Max) from a Stream

You can create a single step operation to compute the minimum and maximum values of a stream:

package org.kodejava.util.stream;

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

public class TeeingStatsExample {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(3, 5, 7, 2, 8);

        var stats = numbers.stream()
                .collect(Collectors.teeing(
                        Collectors.minBy(Integer::compareTo),     // Collector 1: Get Min
                        Collectors.maxBy(Integer::compareTo),     // Collector 2: Get Max
                        (min, max) -> new int[]{min.orElse(-1), max.orElse(-1)} // Merge into an array
                ));

        System.out.println("Min: " + stats[0] + ", Max: " + stats[1]);
        // Output: Min: 2, Max: 8
    }
}

How It Works:

  • Two collectors (downstream1 and downstream2) collect the stream elements independently. For example, the first collector might compute the sum, while the second computes the average.
  • Once the stream has been fully processed, the results from both collectors are passed to the merger, which applies a transformation or combination of the two results.

Example 3: Concatenate Strings and Count Elements Simultaneously

Here’s how you can process a stream of strings to count the number of elements and also concatenate them into a single string:

package org.kodejava.util.stream;

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

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

        var result = names.stream()
                .collect(Collectors.teeing(
                        Collectors.joining(", "),        // Collector 1: Concatenate strings
                        Collectors.counting(),           // Collector 2: Count elements
                        (joined, count) -> joined + " (Total: " + count + ")" // Merge
                ));

        System.out.println(result);  // Output: Alice, Bob, Charlie (Total: 3)
    }
}

Key Points:

  1. Stream Processing: The stream elements are processed only once but collected using two different collectors.
  2. Merger Function: The merger combines both results into a final result of your choice.
  3. Utility: Collectors::teeing is very useful when you need to perform dual aggregations in one pass over the data.

Now you’re ready to use Collectors::teeing for combining results in your streams!

How do I use List.of, Set.of, and Map.of factory methods?

The List.of, Set.of, and Map.of factory methods were introduced in Java 9 to create immutable collections in a simpler and more concise way. These methods directly create unmodifiable instances of List, Set, or Map.

Usage of List.of

The List.of method is used to create an unmodifiable List containing the provided elements. The key characteristics are:

  • The list is immutable, meaning you cannot modify its contents (e.g., add, remove, replace elements).
  • If you attempt to modify the list, a java.lang.UnsupportedOperationException will be thrown.

Key Points:

  1. It is null-safety aware, meaning null is not allowed as an element.
  2. The created list maintains the insertion order.

Example

List<String> names = List.of("Rosa", "John", "Mary", "Alice");
System.out.println(names); // Output: [Rosa, John, Mary, Alice]

names.add("Bob"); // Throws java.lang.UnsupportedOperationException

Note: Attempting to pass a null element will result in NullPointerException.


Usage of Set.of

The Set.of method creates an immutable Set containing the given elements. The key characteristics are:

  • The set is unmodifiable, so you cannot add or remove elements after creation.
  • It does not allow duplicate elements.
  • It does not allow null elements.

Key Points:

  1. A java.lang.IllegalArgumentException is thrown if duplicate elements are provided during creation.
  2. Since a Set does not guarantee order, the order of elements in the resulting set is not predictable.

Example

Set<String> items = Set.of("apple", "banana", "orange");
System.out.println(items); // Output: [apple, banana, orange] (order may vary)

items.add("kiwi"); // Throws java.lang.UnsupportedOperationException

Note: If duplicate elements are passed, an exception will be thrown:

Set.of("apple", "banana", "apple"); // Throws IllegalArgumentException

Usage of Map.of

The Map.of method creates an immutable Map with key-value pairs. The characteristics are:

  • The created map is unmodifiable.
  • Both null keys and null values are not allowed.
  • Duplicate keys are not allowed, and attempting to use duplicate keys will throw IllegalArgumentException.

Example

Map<String, Integer> map = Map.of("John", 25, "Mary", 30, "Alice", 27, "Rosa", 22);

System.out.println(map); 
// Output (order may vary): {John=25, Mary=30, Alice=27, Rosa=22}

map.put("Bob", 31); // Throws java.lang.UnsupportedOperationException

Note: Avoid duplicate keys. For example:

Map.of("Key1", 1, "Key2", 2, "Key1", 3); // Throws IllegalArgumentException

Advantages of List.of, Set.of, and Map.of

  1. Immutable collections help ensure thread safety without additional synchronization.
  2. Improved code conciseness compared to using Collections.unmodifiableList, Collections.unmodifiableSet, or Collections.unmodifiableMap.
  3. Simplified initialization with a clean and readable API.

For larger maps (more than 10 entries), use Map.ofEntries for better clarity:

Map<String, Integer> largeMap = Map.ofEntries(
    Map.entry("Alice", 27),
    Map.entry("John", 25),
    Map.entry("Mary", 30),
    Map.entry("Rosa", 22)
);

How do I use the RandomGenerator API introduced in JDK 17?

The RandomGenerator API, introduced in JDK 17, provides a simpler and more flexible way to work with random number generation by centralizing the different strategies for producing random numbers under a single interface (RandomGenerator). This allows developers to access multiple random number generation algorithms in a standardized manner. Additionally, it improves upon the legacy java.util.Random class.

Here’s how you can use the RandomGenerator API:

Key Classes/Interfaces in the RandomGenerator API

  • RandomGenerator (Interface): Defines methods for generating random values of different types (e.g., nextInt(), nextDouble(), etc.).
  • RandomGeneratorFactory (Class): Used to instantiate various implementations of the RandomGenerator interface.
  • SplittableRandom, ThreadLocalRandom, SecureRandom: Core implementations of RandomGenerator.
  • Random: Although part of the legacy API, it now implements RandomGenerator in JDK 17.

Code Example: Basic Usage with RandomGenerator

Here’s a basic example of how to use RandomGenerator:

package org.kodejava.util.random;

import java.util.random.RandomGenerator;

public class RandomGeneratorExample {
    public static void main(String[] args) {
        // Retrieve the default random generator
        RandomGenerator generator = RandomGenerator.getDefault();

        // Generate random numbers of various types
        int randomInt = generator.nextInt();          // Random integer
        double randomDouble = generator.nextDouble(); // Random double in [0.0, 1.0)
        long randomLong = generator.nextLong();       // Random long
        boolean randomBoolean = generator.nextBoolean(); // Random boolean

        // Print results
        System.out.println("Random integer: " + randomInt);
        System.out.println("Random double: " + randomDouble);
        System.out.println("Random long: " + randomLong);
        System.out.println("Random boolean: " + randomBoolean);

        // Generate a random integer within a range (0 to 100)
        int rangedInt = generator.nextInt(101);
        System.out.println("Random integer in range [0, 100]: " + rangedInt);
    }
}

Using RandomGeneratorFactory for Choosing a Specific Algorithm

The RandomGeneratorFactory class allows you to select specific implementations of RandomGenerator. This can help you use different algorithms tailored to your use case.

Example:

package org.kodejava.util.random;

import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;

public class CustomRandomGeneratorExample {
    public static void main(String[] args) {
        // List all available random generator algorithms
        System.out.println("Available Random Generators:");
        RandomGeneratorFactory.all().forEach(factory -> {
            System.out.println("- " + factory.name());
        });

        // Create a specific random generator (e.g., `L64X128MixRandom`)
        RandomGenerator generator = RandomGeneratorFactory.of("L64X128MixRandom").create();

        // Generate random values
        System.out.println("Random value: " + generator.nextDouble());
    }
}

Notes

  1. Default Generator: RandomGenerator.getDefault() provides a default random number generator.
  2. Thread-Safety: Consider java.util.concurrent.ThreadLocalRandom for generating random numbers in a multithreaded context.
  3. Secure Random Numbers: Use java.security.SecureRandom for cryptographically secure random numbers.
  4. Performance: If you need high-performance statistically random values, explore generators like L128X256MixRandom.

Benefits of Using the RandomGenerator API

  • Multiple Algorithms: No need to rely solely on java.util.Random.
  • Improved Extensibility: Selecting different implementations for specific use cases is easier.
  • Consistency: Unified method signatures across implementations enable flexible and consistent code.

How do I use Optional for cleaner null checks?

Using Optional in Java can help streamline and simplify null checks, avoiding potential NullPointerException issues and making the code more readable and elegant. Optional is particularly useful when you want to express the possibility of an absent value explicitly in the API and handle such scenarios gracefully.

Here’s how you can use Optional for cleaner null checks:


1. Creating an Optional

You can create an Optional object to wrap either a non-null or null value.

Optional<String> optionalValue = Optional.of("example"); // Non-null value
Optional<String> emptyOptional = Optional.empty();       // Explicit empty optional
Optional<String> nullableOptional = Optional.ofNullable(null); // Can be null

2. Using isPresent() for Checks

Instead of if (value != null), you can use isPresent() to check for a value’s presence:

Optional<String> optionalValue = Optional.ofNullable("example");
if (optionalValue.isPresent()) {
    System.out.println("Value is present: " + optionalValue.get());
}

3. Using ifPresent() for Action

If you want to perform some operation only if a value is present, you can use ifPresent():

optionalValue.ifPresent(value -> System.out.println("Found: " + value));

This eliminates the need for explicit if checks.


4. Provide a Default Value with orElse()

You can supply a default value to use if the Optional is empty:

String result = optionalValue.orElse("Default Value");
System.out.println(result);

5. Lazy Default Value with orElseGet()

To defer the computation of the default value:

String result = optionalValue.orElseGet(() -> "Generated Default");
System.out.println(result);

6. Throw an Exception if Absent with orElseThrow()

You can ensure an exception is thrown when the value is absent:

String value = optionalValue.orElseThrow(() -> new IllegalArgumentException("Value is missing!"));

7. Transforming the Value with map()

Use map() to apply a transformation function to the contained value, without needing to check for null:

Optional<Integer> length = optionalValue.map(String::length);
length.ifPresent(len -> System.out.println("Length: " + len));

8. Chained Operations with flatMap()

If the transformation itself returns an Optional, use flatMap() to avoid nesting:

Optional<String> toUpperCaseOptional = optionalValue.flatMap(value -> Optional.of(value.toUpperCase()));
toUpperCaseOptional.ifPresent(System.out::println);

9. Filtering Values

You can filter the value based on a condition:

optionalValue.filter(value -> value.length() > 5)
             .ifPresent(value -> System.out.println("Value with sufficient length: " + value));

10. Combining Operations

Combine operations like map, filter, and orElse to handle cases cleanly in a pipeline:

String finalValue = optionalValue
                        .map(String::toUpperCase)
                        .filter(value -> value.startsWith("EX"))
                        .orElse("Default Result");
System.out.println(finalValue);

Common Use Cases:

  • Avoid nullable parameters in methods by using Optional.
  • Indicate that a return value may or may not be present, eliminating null checks on the client side.
  • Use in streams to safely process values.

By following these practices with Optional, you can reduce boilerplate code and improve the overall clarity of null safety in Java applications.