Skip to content

Instantly share code, notes, and snippets.

@taichi
Created October 13, 2015 15:50
Show Gist options
  • Save taichi/f3133621673eaf92edbe to your computer and use it in GitHub Desktop.
Save taichi/f3133621673eaf92edbe to your computer and use it in GitHub Desktop.
/**
* @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