Last Updated:

New String.transform() method - Java

It is easier to understand this with an example:


String test = "test";
Integer count = test. transform(s -> s. length());
System. out. println("String length: " + count);

In the method, we passed a function that transforms the object of the type into an object of the class . As you have already noticed, the returned type does not have to be .


package en.javalessons.examples.java12;

public class StringTransform {

public static void main (string [] args ){

String test = "test"

Integer count - test.transform (s -> s.length ()};

System.out.printin ("Any Words: " + Count);



In this example, we only call the passed string method to find out its length. But in practice, you may find this method useful if you want to apply different transforming functions to a string.