Lambda Expression with Primitive Types in Java: Performance Impact?

Ish MishraIsh Mishra
2 min read

A significant feature introduced for developers in Java 8+ is the language-level support for lambda expressions.

When using lambda expressions, we aim to avoid a feature introduced in Java 5 called autoboxing and unboxing. This occurs when dealing with primitive data types in Java, as the compiler automatically converts a primitive type to its corresponding object (wrapper class) and vice versa, depending on the code.

When implementing lambda expressions, using primitive data types can lead to a performance hit due to the additional processing involved in autoboxing and unboxing.

The java.util.function provides a set of functional interfaces to play around with primitive data types. Let us see a few:

IntSupplier

@FunctionalInterface
public interface IntSupplier {

/**
* Gets a result.
*
* @return a result
*/ int getAsInt();
}

Implementation:

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);
}
}

Implementation:

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); };
}
}

Implementation:

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);
}

Implementation:

DoubleToIntFunction doubleToIntFunction = (someDouble) ->
(int) Math.floor(someDouble);

System.out.println("Value as an int is: " + doubleToIntFunction.applyAsInt(25.5));

Explore the rest of them.

0
Subscribe to my newsletter

Read articles from Ish Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ish Mishra
Ish Mishra

"Welcome to Bits8Byte. I'm Ish, a passionate software engineer with a deep love for technology and a knack for problem-solving. Through this blog, I aim to share my insights, experiences, and discoveries in the ever-evolving world of software development. Having worked in the industry for 9 years, I have had the opportunity to explore various programming languages, frameworks, and tools. I believe in continuous learning and strive to stay up-to-date with the latest industry trends and best practices. In this blog, you can expect to find practical tips, tutorials, and thought-provoking articles. I will also delve into the challenges faced in software development and share my insights on overcoming them. I encourage you to join the conversation by leaving comments, asking questions, and sharing your own experiences. Together, we can grow and inspire each other in our software development journeys.