Lambda Expression with Primitive Types in Java: Performance Impact?

IntSupplier

@FunctionalInterface
public interface IntSupplier {

/**
* Gets a result.
*
*
@return a result
*/
int getAsInt();
}
IntSupplier intSupplier = () -> 18;
int eligibleAgeForVoting = intSupplier.getAsInt();
System.out.println(eligibleAgeForVoting);

IntPredicate

@FunctionalInterface
public interface IntPredicate {

/**
* Evaluates this predicate on the given argument.
*
*
@param value the input argument
*
@return {@code true} if the input argument matches the predicate,
* otherwise {
@code false}
*/
boolean test(int value);
default IntPredicate and(IntPredicate other) {
Objects.requireNonNull(other);
return (value) -> test(value) && other.test(value);
}
default IntPredicate negate() {
return (value) -> !test(value);
}
default IntPredicate or(IntPredicate other) {
Objects.requireNonNull(other);
return (value) -> test(value) || other.test(value);
}
}
IntPredicate isEligibleToVote = (age) -> age >= 18;
boolean isVoting=isEligibleToVote.test(20);
System.out.println(isVoting);

IntConsumer

@FunctionalInterface
public interface IntConsumer {

/**
* Performs this operation on the given argument.
*
*
@param value the input argument
*/
void accept(int value);


default IntConsumer andThen(IntConsumer after) {
Objects.requireNonNull(after);
return (int t) -> { accept(t); after.accept(t); };
}
}
IntConsumer intConsumer =(x)-> System.out.println(x);
intConsumer.accept(25);

DoubleToIntFunction

@FunctionalInterface
public interface DoubleToIntFunction {

/**
* Applies this function to the given argument.
*
*
@param value the function argument
*
@return the function result
*/
int applyAsInt(double value);
}
DoubleToIntFunction doubleToIntFunction = (someDouble) -> 
(int) Math.floor(someDouble);
System.out.println("Value as an int is: " + doubleToIntFunction.applyAsInt(25.5));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store