Java Optional type in Chinese can be paraphrased as “Java Optional 类型”

import java.util.Optional;

public class Main {
    void exec(Optional<StringBuilder> pSb) {

        System.out.println("pSb-->" + pSb);

        StringBuilder sbEdit = pSb.orElseGet(() -> new StringBuilder("none")); // 
        System.out.println("orElseGet-->" + sbEdit ); -- "none"

        // 引数の編集
        Optional<String> os = pSb.map(sb -> {
            sb.insert(0, "[");
            sb.append("]");
            return sb.toString();
        });
    }

    public static void main(String... args) {
        Main main = new Main();
        // foo
        StringBuilder sb = new StringBuilder("Jack");
        sb = null;
        main.exec(Optional.ofNullable(sb ));
        System.out.println("return-->" + sb ); -- "[none]"

    }
}

广告
将在 10 秒后关闭
bannerAds