FunctionUtils { @SuppressWarnings("unchecked") public static <T, R, E extends Exception> Function<T, R> map(ThrowingFunction<T, R, E> function, Function<E, R> exceptionConsumer) { return value -> { try { return function.apply(value); } catch (Exception e) { return exceptionConsumer.apply((E) e); } }; } private  FunctionUtils(); @SuppressWarnings("unchecked") static Function<T, R> map(ThrowingFunction<T, R, E> function, Function<E, R> exceptionConsumer);  }
@Test void map() { List<String> data = Arrays.asList("1", "b", "2"); List<Integer> numbers = data.stream() .map(FunctionUtils.map((ThrowingFunction<String, Integer, NumberFormatException>) Integer::parseInt, e -> -1)) .collect(Collectors.toList()); Assertions.assertEquals(Arrays.asList(1, -1, 2), numbers); }