Created
April 13, 2015 03:39
-
-
Save tomoh1r/5a706039a053213ef5a6 to your computer and use it in GitHub Desktop.
String switch と Optional の連携
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
サンプルコードはこちら 文字列でswitchするときはif-else switchイデオムを使うほうが安全 - きしだのはてな 参考にさせていただきました。