java8函數(shù)編程--Function

Function

Function是java8的一個函數(shù)式編程接口

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     *
     * @param <T> the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

apply方法接收一個入?yún)万俗,返回R,在使用上和理解上類似于我們上學時學的函數(shù) y=f(x)饮怯,可以將我們定義的函數(shù)方法作用在入?yún)上闰歪,函數(shù)的返回值就是R

public class FunctionPractice {
    
    public static <T> Integer getMaxAgeFromClassRoom(T t,Function<T,Integer> func){
        return func.apply(t);
    }
}

public class ClassRoom {

    List<Student> studentList ;
    public ClassRoom() {
         studentList = new ArrayList<>();
         Student s1 = new Student();
         s1.setAge(10);
         s1.setName("s1");

         Student s2 = new Student();
         s2.setAge(11);
         s2.setName("s2");
         studentList.add(s1);
         studentList.add(s2);
     }


    public  Integer getStudentAgeByName(){
        if(CollectionUtils.isEmpty(studentList)){
            return null;
        }
        studentList.sort(Comparator.comparing(Student::getAge).reversed());

        return studentList.get(0).getAge();
    }
}
public class FunctionTest {

    @Test
    public void test(){
        ClassRoom classRoom = new ClassRoom();
        Integer age = FunctionPractice.getMaxAgeFromClassRoom(classRoom,ClassRoom::getStudentAgeByName);
        System.out.println("max age = "+age);
    }
}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市硕淑,隨后出現(xiàn)的幾起案子课竣,更是在濱河造成了極大的恐慌,老刑警劉巖置媳,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件于樟,死亡現(xiàn)場離奇詭異,居然都是意外死亡拇囊,警方通過查閱死者的電腦和手機迂曲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寥袭,“玉大人路捧,你說我怎么就攤上這事〈疲” “怎么了杰扫?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長膘掰。 經(jīng)常有香客問我章姓,道長,這世上最難降的妖魔是什么识埋? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任凡伊,我火速辦了婚禮,結果婚禮上窒舟,老公的妹妹穿的比我還像新娘系忙。我一直安慰自己,他們只是感情好惠豺,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布银还。 她就那樣靜靜地躺著,像睡著了一般洁墙。 火紅的嫁衣襯著肌膚如雪见剩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天扫俺,我揣著相機與錄音苍苞,去河邊找鬼。 笑死狼纬,一個胖子當著我的面吹牛羹呵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播疗琉,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼冈欢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了盈简?” 一聲冷哼從身側響起凑耻,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤太示,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后香浩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體类缤,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年邻吭,在試婚紗的時候發(fā)現(xiàn)自己被綠了餐弱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡囱晴,死狀恐怖膏蚓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情畸写,我是刑警寧澤驮瞧,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站枯芬,受9級特大地震影響剧董,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜破停,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一翅楼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧真慢,春花似錦毅臊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至朗鸠,卻和暖如春蚯撩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背烛占。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工胎挎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人忆家。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓犹菇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親芽卿。 傳聞我的和親對象是個殘疾皇子揭芍,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,835評論 0 38
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,034評論 0 2
  • 最近看了慕課網(wǎng)的《SpringBoot2.0不容錯過的新特性 WebFlux響應式編程》教程,里面介紹了關于Jav...
    逆水尋洲閱讀 1,086評論 0 1
  • 函數(shù)只定義一次卸例,但可能被執(zhí)行或調(diào)用任意次称杨。JS函數(shù)是參數(shù)化的肌毅,函數(shù)的定義會包括一個稱為形參的標識符列表,這些參數(shù)在...
    PySong閱讀 856評論 0 0
  • 函數(shù)只定義一次姑原,但可能被執(zhí)行或調(diào)用任意次悬而。JS函數(shù)是參數(shù)化的,函數(shù)的定義會包括一個稱為形參的標識符列表页衙,這些參數(shù)在...
    PySong閱讀 531評論 0 0