Java Stream How to - Concatenate with method reference








The following code shows how to concatenate with method reference.

Example

// w ww .  ja  va 2s  .  co  m
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class Main {

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

   }
   
   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.