Skip to content

Instantly share code, notes, and snippets.

@diogoarm
Created June 28, 2020 23:04
Show Gist options
  • Save diogoarm/67a0f0e9c483e93c62495daee62ecf50 to your computer and use it in GitHub Desktop.
Save diogoarm/67a0f0e9c483e93c62495daee62ecf50 to your computer and use it in GitHub Desktop.
Functional interface to use with streams, it changes checked exception to unchecked.
package ...
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Throwable> {
R apply(T t) throws E;
static <T, R, E extends Throwable> Function<T, R> unchecked(ThrowingFunction<T, R, E> f) {
return t -> {
try {
return f.apply(t);
} catch (Throwable e) {
throw new RuntimeException(e);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment