Skip to content

Instantly share code, notes, and snippets.

@benweidig
Created May 16, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benweidig/db28981dde2a20f0976a0fd6042ef6be to your computer and use it in GitHub Desktop.
Save benweidig/db28981dde2a20f0976a0fd6042ef6be to your computer and use it in GitHub Desktop.
@FunctionalInterface
interface ExFunction<T, R, E extends Exception> {
R apply(T t) throws E;
}
<T, R, E extends Exception> Function<T, R> uncheck(ExFunction<T, R, E> fn) {
return arg -> {
try {
return fn.apply(arg);
}
catch (Exception e) {
throw new RuntimeException(e);
}
};
}
// Now we can build a stream
public void doWork(List<MyBean> beans) {
beans.stream()
.map(uncheck(bean -> ...))
.forEach(uncheck(item -> ...));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment