How do I use flatMap() method of Optional object?

The flatMap method is a special method in the Optional class in Java, if a method returns an Optional, you can use flatMap to avoid nested Optional<Optional<T>> situations.

Here is an example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalFlatMap {
    public static void main(String[] args) {
        Optional<String> nonEmptyGender = Optional.of("male");
        Optional<String> emptyGender = Optional.empty();

        System.out.println("Non-Empty Optional:: " + nonEmptyGender.flatMap(OptionalFlatMap::getGender));
        System.out.println("Empty Optional:: " + emptyGender.flatMap(OptionalFlatMap::getGender));
    }

    static Optional<String> getGender(String gender) {
        if (gender.equals("male")) {
            return Optional.of("Gender is male");
        } else if (gender.equals("female")) {
            return Optional.of("Gender is female");
        } else {
            return Optional.empty();
        }
    }
}

In this example, two Optional<String> objects are created: one with a value (nonEmptyGender) and one without a value (emptyGender).

The flatMap method is used to apply the method getGender to the value of each Optional<String> (if it exists). Since getGender returns an Optional<String>, using flatMap avoids creating Optional<Optional<String>> objects, and instead directly returns an Optional<String>, that we can easily consume.

The getGender method returns an Optional object, that describes the gender if it is “male” or “female”, or an empty Optional if the gender is neither “male” nor “female”.

The result of calling flatMap will hence be an Optional<String> describing the gender if the gender is “male” or “female”, or an empty Optional in all other cases. This applies to both the non-empty and the empty Optional<String> in the example.

The final output will be:

Non-Empty Optional:: Optional[Gender is male]
Empty Optional:: Optional.empty

In both cases, note that flatMap directly returns the result of getGender, which itself is an Optional. This is different from if map was used, which would have resulted in a nested Optional.

How do I use filter() method of Optional object?

The java.util.Optional class in Java provides a filter method. It’s used to apply a condition on the value held by this Optional.

Here is an example of how to use Optional‘s filter method:

package org.kodejava.util;

import java.util.Optional;

public class OptionalFilter {
    public static void main(String[] args) {

        // Creating Optional object and assigning a value
        Optional<String> myOptional = Optional.of("Hello");

        // Applying filter method on Optional
        Optional<String> result = myOptional.filter(value -> value.length() > 5);

        // Print the result
        // This will not print anything because the length of "Hello" 
        // is not greater than 5.
        result.ifPresent(System.out::println);
    }
}

In this example, the filter method is used to apply a condition on the value held by this myOptional object. The condition is that the length of the value should be greater than 5. If the value satisfies the condition, it is returned. Otherwise, an empty Optional object is returned.

The ifPresent method is used to print the value held by this Optional, if it is non-empty. This particular use of filter will not print anything because the string “Hello” length is not greater than 5.

You can use isEmpty method to check whether Optional is empty.

if (result.isEmpty()) {
   System.out.println("The Optional is empty");
}

In this case, it would print “The Optional is empty”.

How do I use String.join() method in Java?

The String.join() method in Java is a static method added in Java 8 to the java.lang.String class. The String.join() is a static utility method used to concatenate multiple strings, arrays or collections (like lists and sets) of strings. This method makes it easier to join multiple strings with a specific delimiter. A delimiter is a sequence of characters used to separate strings.

This method returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. This method saves us from writing boilerplate loop code just for concatenating strings with a delimiter.

Here is an example of how you can use it:

package org.kodejava.lang;

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

public class StringJoinList {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("Java", "is", "cool");
        String result = String.join(" ", list);
        System.out.println(result);
    }
}

Output:

Java is cool

In this example, String.join() takes two parameters:

  1. A delimiter that is a CharSequence (like a String) that is placed between each joined String.
  2. An Iterable object like a List or a Set, over which the method iterates and joins all elements into a single String.

You can also use String.join() with an array of elements:

package org.kodejava.lang;

public class StringJoinArray {
    public static void main(String[] args) {
        String[] array = new String[]{"Java", "is", "cool"};
        String result = String.join(" ", array);
        System.out.println(result);
    }
}

Output:

Java is cool

In this case, String.join() still takes a delimiter as the first argument, but the second argument is an Array of elements to be joined.

How do I use java.util.Optional class?

The java.util.Optional<T> class is a container object that may or may not contain a non-null value. It was introduced in Java 8 as part of the Java language’s growing emphasis on treating null values as an anti-pattern. Optional is a way of replacing a nullable T reference with a non-null but potentially empty Optional<T> reference.

In functional terminology, Optional is a monadic sequence of operations that can be combined to work with data in a declarative way, while deferring some operations, such as computations on elements.

Here are some useful methods that Optional class provides:

  1. Optional.of(T value): Returns an Optional with the specified present non-null value.
  2. Optional.empty(): Returns an empty Optional instance.
  3. Optional.ofNullable(T value): Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
  4. get(): If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
  5. isPresent(): Returns true if there is a value present, otherwise false.
  6. ifPresent(Consumer<? super T> consumer): If a value is present, invokes the specified consumer with the value, otherwise does nothing.
  7. orElse(T other): Returns the value if present, otherwise returns other.
  8. orElseGet(Supplier<? extends T> other): Returns the value if present, otherwise returns the result produced by the supplying function.
  9. orElseThrow(Supplier<? extends X> exceptionSupplier): If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.

In practical terms, using Optional can help make your code more robust and reduce the likelihood of NullPointerException.

Here is a simple example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalIntroduction {
    public static void main(String[] args) {
        Optional<String> opt = Optional.of("Hello, world!");
        if (opt.isPresent()) {
            System.out.println(opt.get());
        }
    }
}

This program will output: Hello, world!

We can utilize functional-style programming by using ifPresent() method provided by the Optional class. Here’s how:

package org.kodejava.util;

import java.util.Optional;

public class OptionalIfPresent {
    public static void main(String[] args) {
        Optional<String> opt = Optional.of("Hello, world!");
        opt.ifPresent(System.out::println);
    }
}

In this example, opt.ifPresent(System.out::println); is used to print the value of opt if it is present. The System.out::println syntax is a method reference in Java 8 that is functionally equivalent to value -> System.out.println(value). It will only execute System.out.println() if opt is not empty. Hence, it can be considered functional-style programming.

Here are another code snippet on using other methods from the java.util.Optional class:

package org.kodejava.util;

import java.util.Optional;

public class OptionalExample {
    public static void main(String[] args) {
        // Creating Optional objects
        // 1. Creates an empty Optional
        Optional<String> empty = Optional.empty();
        // 2. Creates an Optional with a non-null value
        Optional<String> nonEmpty = Optional.of("Hello");
        // 3. Creates an Optional with a null value
        Optional<String> nullable = Optional.ofNullable(null);

        // isPresent()
        // 1. Output: true
        System.out.println(nonEmpty.isPresent());
        // 2. Output: false
        System.out.println(empty.isPresent());

        // ifPresent()
        // 1. Output: Value is present: Hello
        nonEmpty.ifPresent(value -> System.out.println("Value is present: " + value));
        // 2. No output, since the Optional is empty.
        empty.ifPresent(value -> System.out.println("Value is present: " + value));

        // orElse()
        String valueFromNonEmpty = nonEmpty.orElse("Default Value");
        String valueFromEmpty = empty.orElse("Default Value");
        // Output: Hello
        System.out.println(valueFromNonEmpty);
        // Output: Default Value
        System.out.println(valueFromEmpty);

        // orElseGet()
        String valueFromNonEmptyWithSupplier = nonEmpty.orElseGet(() -> "Default Value");
        String valueFromEmptyWithSupplier = empty.orElseGet(() -> "Default Value");
        // Output: Hello
        System.out.println(valueFromNonEmptyWithSupplier);
        // Output: Default Value
        System.out.println(valueFromEmptyWithSupplier);

        // orElseThrow() when value is present it will return the value
        try {
            String value = nonEmpty.orElseThrow(IllegalArgumentException::new);
            System.out.println(value);
        } catch (IllegalArgumentException e) {
            //Handle exception
            e.printStackTrace();
        }
        // orElseThrow() when value is not present, it throws an exception
        try {
            String value = empty.orElseThrow(IllegalArgumentException::new);
            System.out.println(value);
        } catch (IllegalArgumentException e) {
            //Handle exception
            e.printStackTrace();
        }

    }
}

Output:

true
false
Value is present: Hello
Hello
Default Value
Hello
Default Value
Hello
java.lang.IllegalArgumentException
    at java.base/java.util.Optional.orElseThrow(Optional.java:403)
    at org.kodejava.util.OptionalExample.main(OptionalExample.java:53)

These methods are used to help in providing a more elegant way to handle null values in Java. Make sure to understand how and when to use each method to get the most out of the Optional class.

How do I use Collectors.partitioningBy() method?

Collectors.partitioningBy() is a special case of a grouping collector in Java’s Stream API. It partitions or divides the input elements into two groups, based on the result of a Predicate function. One group for which the Predicate function returns true, and the other where it returns false.

Each group is a List of elements, and the method returns a Map where the keys are Boolean values (true and false), and the values are the resulting groups.

Here’s a simple example:

package org.kodejava.stream;

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

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

        Map<Boolean, List<Integer>> isEven = numbers.stream()
                .collect(Collectors.partitioningBy(num -> num % 2 == 0));

        System.out.println("Partition of Even Numbers: " + isEven.get(true));
        System.out.println("Partition of Odd Numbers: " + isEven.get(false));
    }
}

Output:

Partition of Even Numbers: [2, 4, 6, 8, 10]
Partition of Odd Numbers: [1, 3, 5, 7, 9]

In this example, we’re partitioning a list of integers into even and odd numbers. The Predicate function num -> num % 2 == 0 returns true for even numbers and false for odd numbers.

The value that partitioningBy() method returns is a Map where the key true maps to a list of numbers for which the Predicate was true (even numbers), and the key false maps to a list of numbers for which the Predicate was false (odd numbers).

You can use partitioningBy() to easily categorize elements of a stream where the categorization criterion can be represented with a boolean value (i.e., you have a binary condition to divide your elements).