In Java, transform(f) calls the function f with the string as its argument and returns the result: s.transform(f) is the same as f.apply(s). The result can be any type, not just String.
Its value is readability: you can apply your own helper methods in the same left-to-right chain as built-in methods, instead of wrapping calls inside each other. The parameter type is Function<? super String, ? extends R>, so lambdas and method references both fit.
transform() is an instance method of java.lang.String, added in Java 12. Call it on a string, as in text.transform(...); calling it on a null reference throws NullPointerException. Requires Java 12 or newer.
Syntax
Pass a lambda or a method reference that accepts a String.
java
String clean = raw.transform(String::strip);
int number = "42".transform(Integer::parseInt);
Call
What it does
"ab".transform(s -> s + s)
"abab".
"5".transform(Integer::parseInt)
The Integer 5.
"hi".transform(String::length)
2.
transform itself does nothing to the string; all behavior comes from the function you pass. If the function returns a String, you can keep chaining other String methods afterwards.
Library declarations
java
public <R> R transform(Function<? super String, ? extends R> f);
Parameters
Parameter
Description
f
The function to apply to this string.
Return Value
Return Value
Description
R
Whatever f returns (a String, a number, a list, ...).
Java Examples
Example 1 — Chain Clean-Up Steps Left to Right
Strip the input, then uppercase it, as a readable pipeline.
java
import java.util.Locale;
public class TransformChain {
public static void main(String[] args) {
String result = " ada lovelace "
.transform(String::strip)
.transform(s -> s.toUpperCase(Locale.ROOT));
System.out.println("[" + result + "]");
}
}
Output
[ADA LOVELACE]
How It Works
A method reference first.String::strip is a Function<String, String>, so transform applies it and returns the stripped text.
A lambda next. The second step uppercases with Locale.ROOT; each step receives the previous result.
Reads in order. The chain lists the steps in the order they run, instead of nesting f(g(x)).
Example 2 — Convert Text into Other Types
The result type follows the function: here an int and an Integer.
java
public class TransformParse {
public static void main(String[] args) {
int total = "42".transform(Integer::parseInt) + 8;
Integer length = "hello".transform(String::length);
System.out.println(total);
System.out.println(length);
}
}
Output
50
5
How It Works
String in, number out.Integer::parseInt turns "42" into 42, so R is Integer here.
Use it like any number. The result is unboxed and adding 8 prints 50.
Any String method works.String::length returns 5 for "hello"; generics make the result an Integer.
Example 3 — Build a Reusable Slug Function
Store a pipeline in a Function variable and apply it to any text.
java
import java.util.Locale;
import java.util.function.Function;
public class TransformSlug {
public static void main(String[] args) {
Function<String, String> slugify = s -> s.strip()
.toLowerCase(Locale.ROOT)
.replaceAll("[^a-z0-9]+", "-");
System.out.println(" Hello, World! Java 12 ".transform(slugify));
System.out.println("Ready Set Go".transform(slugify.andThen(String::length)));
}
}
Output
hello-world-java-12
12
How It Works
Define once.slugify strips, lowercases, and replaces every run of other chars with one dash.
Apply anywhere.transform(slugify) turns the title into hello-world-java-12.
Compose functions.andThen(String::length) builds a new function that slugifies and then measures; "ready-set-go" has 12 chars.
Frequently Asked Questions
It applies a Function to the string and returns the function result: s.transform(f) equals f.apply(s).
Java 12.
Yes. The return type R comes from the function, so it can return an Integer, a List, or any other type.
It keeps a chain of operations readable from left to right, mixing your own helpers with built-in String methods.
No. Strings are immutable; transform() just passes the string to your function.