0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java Optional型

Last updated at Posted at 2019-11-30
sample.java
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]"
        
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?