Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Supplier::get return null

Tags:

java

java-8

The documentation of Supplier::get is not explicit on the possibility or not for it to return null. Is it possible ?

I thought that it might be the case of a Supplier<Void>. In this case, how can I invoke my Supplier to make sure it does not result in Supplier<Void> ? In other word, which T is such such that it can be anything but Void ?

like image 739
EnzoMolion Avatar asked May 25 '26 19:05

EnzoMolion


1 Answers

As pointed out by @Michael, it most certainly can, as in the following code snippet:

import java.util.function.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Supplier<String> supplier = () -> null;
        System.out.println(supplier.get());
    }
}

It painfully highlighted my poor understanding of coping with high order usage of Provider.

like image 141
EnzoMolion Avatar answered May 27 '26 08:05

EnzoMolion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!