Key Features of LongUnaryOperator
- It performs operations on
long
values. - It has a single abstract method:
long applyAsLong(long operand)
, which takes onelong
argument and returns along
result. - Additional default and static methods such as
andThen()
andcompose()
allow chaining of operations.
Key Methods in LongUnaryOperator
long applyAsLong(long operand)
- Performs the operation and returns the result.
andThen(LongUnaryOperator after)
- Returns a composed
LongUnaryOperator
that applies the current operation, then applies the specifiedLongUnaryOperator
operation.
- Returns a composed
compose(LongUnaryOperator before)
- Returns a composed
LongUnaryOperator
that applies the given operation first, and then the current operation.
- Returns a composed
static LongUnaryOperator identity()
- Returns a
LongUnaryOperator
that returns the input value as-is (acts as an identity function).
- Returns a
Using LongUnaryOperator
in Practice
1. Basic Example
Here is a simple example where we use LongUnaryOperator
to create a lambda that doubles a number:
package org.kodejava.util.function;
import java.util.function.LongUnaryOperator;
public class LongUnaryOperatorExample {
public static void main(String[] args) {
// Lambda to double the value
LongUnaryOperator doubleValue = operand -> operand * 2;
long input = 5L;
// Apply the operation
long result = doubleValue.applyAsLong(input);
System.out.println("Result: " + result);
// Output: 10
}
}
2. Using Method References
We can use method references to refer to a static or instance method that matches the functional signature:
package org.kodejava.util.function;
import java.util.function.LongUnaryOperator;
public class LongUnaryOperatorMethodRef {
public static long square(long x) {
return x * x;
}
public static void main(String[] args) {
// Method reference
LongUnaryOperator squareOperator = LongUnaryOperatorMethodRef::square;
long input = 6L;
// Apply the operation
long result = squareOperator.applyAsLong(input);
System.out.println("Result: " + result);
// Output: 36
}
}
3. Chaining Operations
The andThen()
and compose()
methods can be used to chain operations sequentially.
andThen()
: Executes the current operation first, then the next operation.compose()
: Executes the given operation first, then the current operation.
Example:
package org.kodejava.util.function;
import java.util.function.LongUnaryOperator;
public class LongUnaryOperatorChaining {
public static void main(String[] args) {
LongUnaryOperator increment = operand -> operand + 1;
LongUnaryOperator square = operand -> operand * operand;
// Chain operations: (increment -> square)
LongUnaryOperator incrementThenSquare = increment.andThen(square);
long input = 3L;
// (3 + 1)^2 = 16
long result1 = incrementThenSquare.applyAsLong(input);
System.out.println("Increment then Square Result: " + result1);
// Chain operations: (increment -> square)
LongUnaryOperator incrementComposeSquare = square.compose(increment);
// (3 + 1)^2 = 16
long result2 = incrementComposeSquare.applyAsLong(input);
System.out.println("Increment compose Increment Result: " + result2);
}
}
4. Using with Streams
The LongUnaryOperator
can also be used in contexts like streams, especially LongStream
:
package org.kodejava.util.function;
import java.util.stream.LongStream;
import java.util.function.LongUnaryOperator;
public class LongUnaryOperatorWithStreams {
public static void main(String[] args) {
LongUnaryOperator multiplyByThree = operand -> operand * 3;
LongStream numbers = LongStream.of(1L, 2L, 3L, 4L);
numbers.map(multiplyByThree).forEach(System.out::println);
// Output:
// 3
// 6
// 9
// 12
}
}
5. Example of identity()
:
package org.kodejava.util.function;
import java.util.function.LongUnaryOperator;
public class LongUnaryOperatorIdentity {
public static void main(String[] args) {
LongUnaryOperator identity = LongUnaryOperator.identity();
long input = 25L;
// Input and output are the same
long result = identity.applyAsLong(input);
System.out.println("Identity Result: " + result);
// Output: 25
}
}
Summary
The LongUnaryOperator
is a useful functional interface for performing operations on long
values. It is commonly applied for transformations in data processing workflows, functional programming, and stream pipelines. Its ability to compose and chain operations allows for clean and concise code.