11 November 2020

What is Optional?

This is the Schrödinger's Cat of Java. It represents a datum that might or might not be present. It is a wrapper around its piece of data.

This has become a widespread practice in Java programming and it obviates the need for setting objects equal to null. One use case is an app that handles a file. To represent the file, you should use an Optional<File> or use an Optional<String> to represent the file name.

Here is how to unwrap the datum.

Effect Optional<T> OptionalInt OptionalLong OptionalDouble
Unwrap object get() getAsInt() getAsLong() getAsDouble()

Here are some methods common to all of the Optional classes.

Stat.java Statistics computed from data often make no sense if they are handed empty lists. Note the use of Optionals here.


/**************************************************
*   Author: Morrison
*   Date:  11 Nov 202020
**************************************************/
import java.util.ArrayList;
import java.util.OptionalDouble;
import java.util.Collections;

public class Stat
{
    
    public static void main(String[] args)
    {
        ArrayList<Double> al = new ArrayList<>();
        al.add(4.0);
        al.add(8.0);
        al.add(2.0);
        OptionalDouble result = mean(al);
        if(result.isPresent())
        {
            System.out.printf("The mean is %s\n", result.getAsDouble());
        }
        else
        {
            System.out.println("Your list was empty");
        }
        al.clear();
        for(int k = 1; k <=5;k++)
        {
            al.add(k*1.0);
        }
        result = median(al);
        if(result.isPresent())
        {
            System.out.printf("The median is %s\n", result.getAsDouble());
        }
        else
        {
            System.out.println("Your list was empty");
        }
        result = standardDeviation(al);
        if(result.isPresent())
        {
            System.out.printf("The standard deviation is %s\n", result.getAsDouble());
        }
        else
        {
            System.out.println("Your list was empty");
        }
        al.add(6.0);
       result = median(al);
        if(result.isPresent())
        {
            System.out.printf("The median is %s\n", result.getAsDouble());
        }
        else
        {
            System.out.println("Your list was empty");
        }
        result = standardDeviation(al);
        if(result.isPresent())
        {
            System.out.printf("The standard deviation is %s\n", result.getAsDouble());
        }
        else
        {
            System.out.println("Your list was empty");
        }
    }

    public static OptionalDouble mean(ArrayList<Double> x)
    {
        if(x.size() == 0)
        {
            return OptionalDouble.empty();
        }
        double total = 0;
        for(double d: x)
        {
            total += d;
        }
        return OptionalDouble.of(total/x.size());
    }
    public static OptionalDouble median(ArrayList<Double> x)
    {

        if(x.size() == 0)
        {
            return OptionalDouble.empty();
        }
        Collections.sort(x);
        int n = x.size();
        double out = n%2 == 1? x.get((n-1)/2):
            .5*(x.get(n/2 - 1) + x.get(n/2));
        return OptionalDouble.of(out);
    }
    public static OptionalDouble standardDeviation(ArrayList<Double> x)
    {
        if(x.size() == 0)
        {
            return OptionalDouble.empty();
        }
        double out = 0;
        double mu = mean(x).getAsDouble();
        for(double d: x)
        {
            out += (d - mu)*(d - mu);
        }
        out /= x.size();
        out = Math.sqrt(out);
        //sqrt((1/n)sum( x[k] - mean)**2)
        return OptionalDouble.of(out);
    }

}