Java Stream How to - Convert string to uppercase with method reference








The following code shows how to convert string to uppercase with method reference.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
/*from  w w  w . j a va  2 s  . co  m*/
public class Main {

   public static void main(String[] args) {
     List<String> names = Arrays.asList("john", "jane", "oliver");
     System.out.println(transformStrings(names, String::toUpperCase));

   }
   
   private static List<String> transformStrings(List<String> names, Function<String, String> fx) {
     List<String> appliedNames = new ArrayList<>();
     for (String n : names) {
        appliedNames.add(fx.apply(n));
     }
     return appliedNames;
  }
}

The code above generates the following result.