Created
January 26, 2017 02:41
-
-
Save masanobuimai/54da8a86e3bd65b490de73e1c3c22407 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Map<String, Function<String, String>> switchCase = new HashMap<>(); | |
switchCase.put("A", String::toLowerCase); | |
switchCase.put("a", String::toUpperCase); | |
Stream.of("A", "a", "aaa", "bbb") | |
.map(str -> Optional.ofNullable(switchCase.get(str)) | |
.map(f -> f.apply(str)) | |
.orElse(str)) | |
.forEach(System.out::println) |
hum...
.map(str -> switchCase.getOrDefault(str, k -> str).apply(str))
.forEach(System.out::println)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eclipse Collectionsの
CaseFunction
を使うとこんな感じにスマートにいけますがどうでしょう。実際のコンテナはStreamでもEclipse Collectionsでもどちらでもいけます。(追記:toUpperCase()の箇所を間違えてたので直しました)