App下載
首頁(yè)javastream_convertJava Stream - 如何鏈函數(shù)來(lái)轉(zhuǎn)換字符串

Java Stream - 如何鏈函數(shù)來(lái)轉(zhuǎn)換字符串

我們想知道如何鏈函數(shù)來(lái)轉(zhuǎn)換字符串。
import java.util.function.Function;
public class Main {
  public static String addHeader(String text){
    return "From w3cschool.cn:" + text;
}

public static String addFooter(String text){
    return text + "Kind regards";
}

public static String checkSpelling(String text){
    return text.replaceAll("a", "*");
}


public static void main(String...args){
    Function<String, String> addHeader = Main::addHeader;
    Function<String, String> transformationPipeline
      = addHeader.andThen(Main::checkSpelling)
                 .andThen(Main::addFooter);

    System.out.println(transformationPipeline.apply("this is a test!"));
}



}