我想知道如何强制立即评估
Java8中的每个map函数?我现在的情况是我需要进行多级转换(来自ObjectA – > ObjectB – > ObjectC – > ObjectD),并且对于某些对象,在此转换的每个级别上都可能存在失败(抛出异常) .例如
// stream -> map -> collect sequence
lists.stream()
.map(aToB)
.collect(Collectors.toList())
.stream()
.map(bToC)
.collect(Collectors.toList())
.stream()
.map(cToD)
.collect(Collectors.toList())
// Try api is from javaslangs
Function aToB = a -> Try.of(() -> .....)
.onFailure(....)
.get();
Function bToC = b -> Try.of(() -> .....)
.onFailure(....)
.get();
Function cToD = c -> Try.of(() -> .....)
.onFailure(....)
.get();
我想测试我的单元测试中的每个转换,例如测试是否测试抛出异常,以及如果A在将A转换为B之类的异常时未成功转换为C,但是使用惰性求值,则无法测试,我能想到的唯一方法是做这个序列的steam() – > map(…) – >收集(…)电话强制立即评估.我想知道是否有更好的方式来写这个.