LoginSignup
0
0

More than 5 years have passed since last update.

Optionalの使い方

Posted at
1 / 2

久し振りにjava使い始めたのでjava8の新機能おさらいがてらの備忘録。

Optinal

Optinal.ofNullable, .of(Optinal t), .empty

ofNullableが便利そう。

sample.java

Optinal<String> _name;

public Optinal<String> getName(){
    return this._name;
};

public void setName(String name){
    // this._name = (name != null) 
    // ? Optional.of(name) 
    // : Optinal.empty;
    this._name = Optinal.ofNullable(name);
}

isPresent, get, ifPresent

ifPresentが便利そう。

sample2.java

public void print(Optinal<String> optinalName){
    // if(optinalName.isPresent()){
    //     SYstem.out.print(optionalName.get());
    // }
    optinalName.ifPresent(name -> System.out.print(name));
}

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