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 Function Interface - Complete Tutorial with Examples Java Function InterfaceLast modified: April 16, 2025
The
Function Interface Overview
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before);
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after);
static <T> Function<T, T> identity();
}
The code above shows the structure of Basic Function UsageThe simplest way to use Function is with lambda expressions. We define how to transform input to output in the apply method. The example converts strings to their lengths. Main.java
package com.zetcode;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// Define a function that takes String and returns its length
Function<String, Integer> lengthFunction = s -> s.length();
// Apply the function
System.out.println("Length of 'hello': " + lengthFunction.apply("hello"));
System.out.println("Length of 'functional': " + lengthFunction.apply("functional"));
// Function using method reference
Function<String, Integer> lengthMethodRef = String::length;
System.out.println("Length via method ref: " + lengthMethodRef.apply("method"));
}
}
This example demonstrates basic Function usage with lambda and method reference. The lengthFunction takes String and returns Integer. We apply it to different strings. Method reference provides more concise syntax for existing methods. Function Composition with andThen
The Main.java
package com.zetcode;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// First function converts String to uppercase
Function<String, String> toUpper = s -> s.toUpperCase();
// Second function extracts first 3 characters
Function<String, String> firstThree = s -> s.substring(0, Math.min(s.length(), 3));
// Compose the functions
Function<String, String> upperThenTrim = toUpper.andThen(firstThree);
System.out.println("Result: " + upperThenTrim.apply("hello world"));
System.out.println("Result: " + upperThenTrim.apply("java"));
}
}
This example shows function composition with Function Composition with compose
The Main.java
package com.zetcode;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// Function to double a number
Function<Integer, Integer> doubler = x -> x * 2;
// Function to increment by 1
Function<Integer, Integer> incrementer = x -> x + 1;
// Compose in different orders
Function<Integer, Integer> incrementThenDouble = doubler.compose(incrementer);
Function<Integer, Integer> doubleThenIncrement = doubler.andThen(incrementer);
System.out.println("Increment then double 5: " + incrementThenDouble.apply(5));
System.out.println("Double then increment 5: " + doubleThenIncrement.apply(5));
}
}
This example demonstrates the difference between Using Function with StreamsFunction is commonly used with Java Streams for data transformation. The map operation accepts a Function to transform stream elements. This enables clean data processing pipelines. Main.java
package com.zetcode;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> names = Arrays.asList("alice", "bob", "charlie", "dave");
// Function to capitalize first letter
Function<String, String> capitalize = s ->
s.substring(0, 1).toUpperCase() + s.substring(1);
// Apply function in stream
List<String> capitalizedNames = names.stream()
.map(capitalize)
.collect(Collectors.toList());
System.out.println("Original: " + names);
System.out.println("Transformed: " + capitalizedNames);
}
}
This example shows Function usage in Streams. We define a capitalization function and apply it to each stream element via map. The result is a new list with transformed values. Stream operations become very expressive. Function Identity
The Main.java
package com.zetcode;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Identity function
Function<String, String> identity = Function.identity();
System.out.println("Identity applied: " + identity.apply("test"));
// Practical use in streams
Stream<String> words = Stream.of("a", "b", "c");
var result = words.collect(Collectors.toMap(
Function.identity(), // key mapper
String::length // value mapper
));
System.out.println("Resulting map: " + result);
}
}
This example demonstrates BiFunction and Beyond
While Function takes one argument, Java provides related interfaces for
different arities. Main.java
package com.zetcode;
import java.util.function.BiFunction;
import java.util.function.DoubleFunction;
import java.util.function.ToIntFunction;
public class Main {
public static void main(String[] args) {
// BiFunction example
BiFunction<Integer, Integer, String> sumToString =
(a, b) -> String.valueOf(a + b);
System.out.println("Sum as string: " + sumToString.apply(5, 3));
// Primitive specialized functions
DoubleFunction<String> doubleFormatter = d -> String.format("$%.2f", d);
System.out.println("Formatted: " + doubleFormatter.apply(12.3456));
ToIntFunction<String> stringToLength = String::length;
System.out.println("Length as int: " + stringToLength.applyAsInt("hello"));
}
}
This example shows SourceJava Function Interface Documentation In this article, we've covered the essential methods and features of the Java Function interface. Understanding these concepts is crucial for functional programming and stream 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. |