GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java LongUnaryOperator Interface - Complete Tutorial with Examples Java LongUnaryOperator InterfaceLast modified: April 16, 2025
The
LongUnaryOperator Interface Overview
@FunctionalInterface
public interface LongUnaryOperator {
long applyAsLong(long operand);
default LongUnaryOperator compose(LongUnaryOperator before);
default LongUnaryOperator andThen(LongUnaryOperator after);
static LongUnaryOperator identity();
}
The code above shows the structure of Basic LongUnaryOperator UsageThe simplest way to use LongUnaryOperator is with lambda expressions. We define how to transform the input long to output long. The example squares input values. Main.java
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Define a function that squares a long value
LongUnaryOperator square = x -> x * x;
// Apply the function
System.out.println("Square of 5: " + square.applyAsLong(5));
System.out.println("Square of 12: " + square.applyAsLong(12));
// Function using arithmetic expression
LongUnaryOperator incrementAndDouble = x -> (x + 1) * 2;
System.out.println("Increment and double 3: " +
incrementAndDouble.applyAsLong(3));
}
}
This example demonstrates basic LongUnaryOperator usage with lambda expressions. The square operator multiplies input by itself. We also show a more complex operation combining increment and multiplication. Results are primitive longs. Function Composition with andThen
The Main.java
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// First function increments by 1
LongUnaryOperator increment = x -> x + 1;
// Second function multiplies by 2
LongUnaryOperator doubler = x -> x * 2;
// Compose the functions
LongUnaryOperator incrementThenDouble = increment.andThen(doubler);
System.out.println("Increment then double 5: " +
incrementThenDouble.applyAsLong(5));
System.out.println("Increment then double 10: " +
incrementThenDouble.applyAsLong(10));
}
}
This example shows function composition with Function Composition with compose
The Main.java
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Function to square a number
LongUnaryOperator square = x -> x * x;
// Function to subtract 5
LongUnaryOperator subtractFive = x -> x - 5;
// Compose in different orders
LongUnaryOperator subtractThenSquare = square.compose(subtractFive);
LongUnaryOperator squareThenSubtract = square.andThen(subtractFive);
System.out.println("Subtract 5 then square 8: " +
subtractThenSquare.applyAsLong(8));
System.out.println("Square then subtract 5 from 8: " +
squareThenSubtract.applyAsLong(8));
}
}
This example demonstrates the difference between Using LongUnaryOperator with StreamsLongUnaryOperator is commonly used with LongStream for primitive long transformations. The map operation accepts a LongUnaryOperator to transform stream elements. This avoids boxing overhead in numeric operations. Main.java
package com.zetcode;
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
// Define transformation operations
LongUnaryOperator square = x -> x * x;
LongUnaryOperator increment = x -> x + 1;
// Apply operations in stream pipeline
LongStream.rangeClosed(1, 5)
.map(square)
.map(increment)
.forEach(System.out::println);
// Combined operation
LongUnaryOperator combined = square.andThen(increment);
System.out.println("Combined result for 3: " +
combined.applyAsLong(3));
}
}
This example shows LongUnaryOperator usage in LongStream. We define square and increment operations and apply them to a range of numbers. The combined operator shows how to chain operations. All operations work with primitive longs. LongUnaryOperator Identity
The Main.java
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Identity function
LongUnaryOperator identity = LongUnaryOperator.identity();
System.out.println("Identity applied to 5: " +
identity.applyAsLong(5));
System.out.println("Identity applied to 100: " +
identity.applyAsLong(100));
// Using identity in stream filter
LongStream.of(10, 20, 30)
.map(LongUnaryOperator.identity())
.forEach(System.out::println);
}
}
This example demonstrates Practical Application: Number FormattingLongUnaryOperator can be used in practical scenarios like number formatting or data transformation pipelines. This example shows a currency conversion operation chain. Main.java
package com.zetcode;
import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args) {
// Conversion rates (simplified)
long usdToEurRate = 85; // 100 USD = 85 EUR
long eurToGbpRate = 90; // 100 EUR = 90 GBP
// Create conversion operators
LongUnaryOperator usdToEur = usd -> usd * usdToEurRate / 100;
LongUnaryOperator eurToGbp = eur -> eur * eurToGbpRate / 100;
// Combined conversion
LongUnaryOperator usdToGbp = usdToEur.andThen(eurToGbp);
long amountInUsd = 20000; // $200.00 in cents
System.out.println("$200 in GBP: £" +
usdToGbp.applyAsLong(amountInUsd) / 100.0);
// Formatting operator
LongUnaryOperator roundToNearest100 = x -> (x + 50) / 100 * 100;
System.out.println("Rounded 1234: " +
roundToNearest100.applyAsLong(1234));
}
}
This example shows practical LongUnaryOperator usage. We create currency conversion operators and chain them together. The rounding operator demonstrates another common numeric operation. All calculations use primitive longs. SourceJava LongUnaryOperator Interface Documentation In this article, we've covered the essential methods and features of the Java LongUnaryOperator interface. Understanding these concepts is crucial for efficient numeric processing in modern Java applications. AuthorMy name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |