Created
October 13, 2015 15:50
-
-
Save taichi/f3133621673eaf92edbe to your computer and use it in GitHub Desktop.
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
/** | |
* @author taichi | |
*/ | |
public class Zy { | |
// これから実装しようとしている処理 | |
public static Optional<Integer> newFn(Optional<String> s) { | |
return s.flatMap(s2 -> { | |
Integer i = funkyOldFn(s2); | |
return i < 0 ? Optional.empty() : Optional.of(i); | |
}); | |
} | |
// 既に実装済でちょっとインタフェースをいじり辛い処理 | |
// 数値に変換できない文字列に対しては、-1を返す | |
public static Integer funkyOldFn(String s) { | |
if (s != null && s.matches("\\d+")) { | |
return Integer.parseInt(s); | |
} | |
return -1; | |
} | |
public static void main(String[] args) { | |
List<String> list = Arrays.asList("1", "a", "3", "4"); | |
list.stream().map(Optional::of).forEach(Zy::newFn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment