Lambda Java8动态调用函数

Lambda Java8动态调用函数,lambda,java-8,Lambda,Java 8,我一直在网上搜索这个问题的例子,但到目前为止我还没有任何运气。我想使用构建器,但不使用对象,而是让构建器“构建”一个使用辅助函数的更复杂的函数 例如,假设我有一个类,它从某个源获取字符串列表(为了简单起见,我手动添加了它们)。然后,一旦我收集了字符串,我想有选择地对这些字符串应用函数。下面的类实际上是伪编码的,但我认为它封装了我想要的行为: public class StringGetter { private List<Function<String, String>

我一直在网上搜索这个问题的例子,但到目前为止我还没有任何运气。我想使用构建器,但不使用对象,而是让构建器“构建”一个使用辅助函数的更复杂的函数

例如,假设我有一个类,它从某个源获取字符串列表(为了简单起见,我手动添加了它们)。然后,一旦我收集了字符串,我想有选择地对这些字符串应用函数。下面的类实际上是伪编码的,但我认为它封装了我想要的行为:

public class StringGetter {

    private List<Function<String, String>> listOfFunctions;

    public List<String> getListOfStrings() {
       List<String> strings = new ArrayList<String>();
       strings.add(" test1");
       strings.add("test2    ");

       listOfFunctions.forEach((function) ->
           // apply function on strings
       )

        return strings;
    }

    public StringGetter withCapitalLetters() {
       listOfFunctions.add(String::toUppserCase);
       return this;
    }

    public StringGetter withTrimmed() {
       listOfFunctions.add(String::trim);
       return this;
    }

    public StringGetter withSingleQuotes() {
       listOfFunctions.add(s -> ' + s + ');
       return this;
    }
}
公共类StringGetter{
私有列表功能;
公共列表getListOfStrings(){
列表字符串=新的ArrayList();
添加(“test1”);
添加(“test2”);
listOfFunctions.forEach((函数)->
//对字符串应用函数
)
返回字符串;
}
公共StringGetter with Capitalletters(){
添加(字符串::touppercase);
归还这个;
}
带修剪()的公共StringGetter{
添加(字符串::trim);
归还这个;
}
带有SingleQuotes()的公共StringGetter{
添加(s->'+s+');
归还这个;
}
}
我怎样才能使它成为我想要的任何助手方法:

StringGetter sg = new StringGetter();

List<String> capitalAndTrimmed = sg.getListOfStrings()
                                        .withCapitalLetters()
                                        .withTrimmed();
System.out.println(capitalAndTrimmed);
Output: ["TEST1", "TEST2"]



List<String> capital = sg.getListOfStrings()
                             .withCapitalLetters()
System.out.println(capital);
Output: [" TEST1", "TEST2   "]



List<String> singleQuotes = sg.getListOfStrings()
                                  .withSingleQuotes();
System.out.println(singleQuotes);
Output: ["' test1'", "'test2    '"]
StringGetter sg=new StringGetter();
List capitalAndTrimmed=sg.getListOfStrings()
.withCapitalLetters()
.withTrimmed();
系统输出打印LN(capitalAndTrimmed);
输出:[“TEST1”、“TEST2”]
List capital=sg.getListOfStrings()
.withCapitalLetters()
系统输出打印号(大写);
输出:[“TEST1”、“TEST2”]
List singleQuotes=sg.getListOfStrings()
.withSingleQuotes();
System.out.println(单引号);
输出:[“'test1'”,“'test2'”]

您可以这样做:

static public class StringGetter {

    private List<Function<String, String>> listOfFunctions = new ArrayList<>();


    public StringGetter withCapitalLetters() {
        listOfFunctions.add(String::toUpperCase);
        return this;
    }

    public StringGetter withTrimmed() {
        listOfFunctions.add(String::trim);
        return this;
    }

    public StringGetter withSingleQuotes() {
        listOfFunctions.add(s -> "'" + s + "'");
        return this;
    }

    public List<String> apply(List<String> input) {
        Function<String, String> one = listOfFunctions.stream()
                .reduce((left, right) -> left.andThen(right))
                .orElse(Function.identity());

        return input.stream().map(one::apply).collect(Collectors.toList());

    }
}
我会将您的实施更改为:

Function<String, String> first = Function.identity();

public StringGetter withCapitalLetters() {
   // may be do a check here so that this is not called twice
   first.andThen(String::toUpperCase);
   return this;
} 

.... other methods

public List<String> apply(List<String> input) {
    return input.stream().map(first::apply).collect(Collectors.toList());
}
Function first=Function.identity();
公共StringGetter with Capitalletters(){
//可以在这里进行检查,这样就不会调用两次
第一个。第二个(String::toUpperCase);
归还这个;
} 
.... 其他方法
公共列表应用(列表输入){
返回input.stream().map(first::apply).collect(Collectors.toList());
}

您应该做一些不同的事情,可能。。。查看
功能#然后
Function<String, String> first = Function.identity();

public StringGetter withCapitalLetters() {
   // may be do a check here so that this is not called twice
   first.andThen(String::toUpperCase);
   return this;
} 

.... other methods

public List<String> apply(List<String> input) {
    return input.stream().map(first::apply).collect(Collectors.toList());
}