Generated from Example_Goal2And3_Test.java at 3.0.0 2020-08-11T17:36:18Z
More primitive types and combinations
Abstract
Basic introduction (by example) to details of main features: More primitive types supported. More combinations of arguments .
More primitive types.
The issue is simple. Functional interface library from JRE contains functional interfaces required by JRE itself. While in most of circumstances what JRE offers is enough, there are obvious and not so obvious limitations:
- sometimes you need more arguments
- sometimes you want use more primitives
sometimes you want all interfaces to have some default and static methods - Function(3) vs ToIntBiFunction (0)
No silver bullet (note about performance)
Do not take this library as a universal tool that improves performance (e.g. by creating more possibilities to avoid capturing lambdas or to avoid boxing/unboxing).
First, there is a lot of cases that actually Java compiler, JIT and GC are already doing a lot of optimizations for you.
Second, diminishing returns might actually mean that any gain you will have is marginal at best.
Basically this library supports:
- More of functional interfaces
- More primitive types are supported
- More combinations of arguments.
- further documentation:
- Thus, reducing number of cases where:
- You cannot directly reference method
- JVM cannot optimize code better (although nothing is guaranteed)
- Default and static utility methods (applicable to the case and availability of other interfaces). read more
Just short example:
private static String typeToString(Float f) {
return f.getClass().getSimpleName();
}
private static String typeToString(float f) {
return "float primitive";
}
@Test
public void someExample() {
LIntToFltFunction toFloat = i -> (float) i;
assertThat(typeToString(toFloat.applyAsFlt(10)))
.isEqualTo("float primitive");
}
Generated from Example_Goal2And3_Test.java at 3.0.0 2020-08-11T17:36:18Z