java8 try with_Java8 stream中使用检查异常

题主希望这段代码能够编译通过

public List getClasses() throws ClassNotFoundException {

List classes =

Stream.of("java.lang.Object", "java.lang.Integer", "java.lang.String")

.map(className -> Class.forName(className))

.collect(Collectors.toList());

return classes;

}

68975504e1e5

image.png

但是lambda表达式中不能有检查异常,必须要try...catch...

public List getClasses() throws ClassNotFoundException {

List classes = Stream.of("java.lang.Object", "java.lang.Integer", "java.lang.String").map(className -> {

try {

return Class.forName(className);

} catch (ClassNotFoundException e) {

throw new RuntimeException(e);

}

}).collect(Collectors.toList());

return classes;

}

看起来很不优雅呀。

于是有个大神写了LambdaExceptionUtil利用泛型的擦除机制

import java.util.function.BiConsumer;

import java.util.function.Consumer;

import java.util.function.Function;

import java.util.function.Supplier;

public class LambdaExceptionUtil {

@FunctionalInterface

public interface Consumer_WithExceptions {

void accept(T t) throws E;

}

@FunctionalInterface

public interface BiConsumer_WithExceptions {

void accept(T t, U u) throws E;

}

@FunctionalInterface

public interface Function_WithExceptions {

R apply(T t) throws E;

}

@FunctionalInterface

public interface Supplier_WithExceptions {

T get() throws E;

}

@FunctionalInterface

public interface Runnable_WithExceptions {

void run() throws E;

}

/**

* .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println));

*/

public static Consumer rethrowConsumer(Consumer_WithExceptions consumer)

throws E {

return t -> {

try {

consumer.accept(t);

} catch (Exception exception) {

throwAsUnchecked(exception);

}

};

}

public static BiConsumer rethrowBiConsumer(

BiConsumer_WithExceptions biConsumer) throws E {

return (t, u) -> {

try {

biConsumer.accept(t, u);

} catch (Exception exception) {

throwAsUnchecked(exception);

}

};

}

/**

* .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName))

*/

public static Function rethrowFunction(Function_WithExceptions function)

throws E {

return t -> {

try {

return function.apply(t);

} catch (Exception exception) {

throwAsUnchecked(exception);

return null;

}

};

}

/**

* rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),

*/

public static Supplier rethrowSupplier(Supplier_WithExceptions function)

throws E {

return () -> {

try {

return function.get();

} catch (Exception exception) {

throwAsUnchecked(exception);

return null;

}

};

}

/**

* uncheck(() -> Class.forName("xxx"));

*/

public static void uncheck(Runnable_WithExceptions t) {

try {

t.run();

} catch (Exception exception) {

throwAsUnchecked(exception);

}

}

/**

* uncheck(() -> Class.forName("xxx"));

*/

public static R uncheck(Supplier_WithExceptions supplier) {

try {

return supplier.get();

} catch (Exception exception) {

throwAsUnchecked(exception);

return null;

}

}

/**

* uncheck(Class::forName, "xxx");

*/

public static R uncheck(Function_WithExceptions function, T t) {

try {

return function.apply(t);

} catch (Exception exception) {

throwAsUnchecked(exception);

return null;

}

}

@SuppressWarnings("unchecked")

private static void throwAsUnchecked(Exception exception) throws E {

throw (E)exception;

}

}

然后就可以这样写了,检查异常不是只能在lambda表达式用`try...catch...``,也能放在方法签名了。

public List getClasses() throws ClassNotFoundException {

List classes =

Stream.of("java.lang.Object", "java.lang.Integer", "java.lang.String")

.map(LambdaExceptionUtil.rethrowFunction(className -> Class.forName(className)))

.collect(Collectors.toList());

return classes;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值