第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

傳遞一些 lambda 作為方法的參數(shù)

傳遞一些 lambda 作為方法的參數(shù)

PIPIONE 2024-01-28 17:06:26
我想將一些 lambda 方法作為參數(shù)傳遞給該方法。不是一個(gè) lambda,而是幾個(gè) lambda。怎么做?flines = arg -> arg.startsWith("WAW");String fname = System.getProperty("user.home") + "/LamComFile.txt"; InputConverter<String> fileConv = new InputConverter<>(fname);List<String> lines = fileConv.convertBy(flines);String text = fileConv.convertBy(flines, join);List<Integer> ints = fileConv.convertBy(flines, join, collectInts);Integer sumints = fileConv.convertBy(flines, join, collectInts, sum);    ...
查看完整描述

1 回答

?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

我認(rèn)為你必須編寫返回類型取決于Function參數(shù)的方法:


class InputConverter<T> {

   private final T value;


    public InputConverter(T value) {

        this.value = value;

    }


    public <R> R convertBy(Function<T, R> function){

        return function.apply(value);

    }

 }

然后您可以Function使用標(biāo)準(zhǔn)方法將參數(shù)組合compose起來andThen:


final String fname = "fname_value"


InputConverter<String> inputConverter = new InputConverter<>(fname);


Function<String, List<String>> valueToListFunction = Arrays::asList;

Function<List<String>, String> firstValueFunction = l -> l.get(0);


List<String> strings = inputConverter.convertBy(valueToListFunction);//[fname_value]

String firstValue = inputConverter.convertBy(

            valueToListFunction

                    .andThen(firstValueFunction)

);

您也可以使用其他標(biāo)準(zhǔn)FunctionalInterfaces,例如UnaryOperator:


UnaryOperator<String> firstChangeFunction = arg -> arg.concat(" + first");

UnaryOperator<String> secondChangeFunction = arg -> arg.concat(" + second");


String firstValue = inputConverter.convertBy(

            valueToListFunction

                    .andThen(firstValueFunction)

                    .andThen(secondChangeFunction)

                    .compose(firstChangeFunction)

); // sout: fname_value + first + second

或者自己寫。


查看完整回答
反對(duì) 回復(fù) 2024-01-28
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)