In Java 8, there is a feature akin to a function-pointer(java.util.function.Function
). It is usually used like this: Function<LookupKey,LookupResult>
, however, there is a problem if the method returns a primitive type. Function<ArgType,Void.TYPE>
doesn't work, it fails to compile with a very confusing error message("cannot find symbol Void.TYPE
"). I would rather avoid changing my method to return an Object
just to pass null as the result.
If your function should not return a value, consider using Consumer<T>
instead.
If your function should return an ìnt
or long
consider using ToIntFunction<T>
or ToLongFunction<T>
which returns primitive types.
If your function should return boolean
, then use Predicate<T>
.
And finally, if you need to consume an primitive type but want to return a reference type use IntFunction<T>
or LongFunction<T>
.
To complete the list: IntToLongFunction
, LongToIntFunction
, IntUnaryOperator
and LongUnaryOperator
support primitive types to be consumed and returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With