Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@tomoh1r 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