當(dāng)前位置:首頁 > IT技術(shù) > 編程語言 > 正文

JAVA中Function的使用
2022-04-25 22:55:36

package com;

import java.util.function.Function;

public class FuctionDemo {
    public static void main(String[] args) {
    convert("100",s -> Integer.parseInt(s),integer ->String.valueOf(integer+100));
    }
    private static void convert(String s, Function<String,Integer> fun1,Function<Integer,String> fun2){
//        Integer s1 = fun1.apply(s);
//        String s2 = fun2.apply(s1);
//        System.out.println(s2);
        String apply = fun1.andThen(fun2).apply(s);
        System.out.println(apply);
    }
}
package com;

import java.util.function.Function;

/**
 * 練習(xí),String s  = "張三,30";
 * 1,將字符串截取得到數(shù)字年齡部分
 * 2,將上一步的年齡字符串轉(zhuǎn)換成int類型數(shù)據(jù)
 * 3,將上一步的int數(shù)據(jù)加70,得到一個int結(jié)果,在控制臺輸出
 */
public class FuctionDemo2 {
    public static void main(String[] args) {
        convert("張三,30",s -> s.split(",")[1],Integer::parseInt,integer -> integer+70);
        convert("張三,30",s -> Integer.parseInt(s.split(",")[1]),integer -> integer+70);
        convert("張三,30",s -> Integer.parseInt(s.split(",")[1])+70);

    }
    public static void convert(String s, Function<String,String>fun1,Function<String ,Integer>fun2,Function<Integer,Integer>fun3){
        Integer apply = fun1.andThen(fun2).andThen(fun3).apply(s);
        System.out.println(apply);
    }
    public static void convert(String s, Function<String,Integer>fun1,Function<Integer ,Integer>fun2){
        Integer apply = fun1.andThen(fun2).apply(s);
        System.out.println(apply);
    }
    public static void convert(String s, Function<String,Integer>fun1){
        Integer apply = fun1.apply(s);
        System.out.println(apply);
    }
}

?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >