Skip to content

Instantly share code, notes, and snippets.

@tomoh1r
Created April 13, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoh1r/5a706039a053213ef5a6 to your computer and use it in GitHub Desktop.
Save tomoh1r/5a706039a053213ef5a6 to your computer and use it in GitHub Desktop.
String switch と Optional の連携
import java.util.Optional;
public class StringSwitch {
public static void main(String[] args) {
String text = null;
switch(Optional.ofNullable(text).orElse("")){
case "a":
System.out.println("AA");
break;
case "b":
System.out.println("BB");
break;
default:
System.out.println("Other");
}
}
}
@tomoh1r
Copy link
Author

tomoh1r commented Apr 13, 2015

サンプルコードはこちら 文字列でswitchするときはif-else switchイデオムを使うほうが安全 - きしだのはてな 参考にさせていただきました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment