在我们的项目中,我们正在迁移到java 8,我们正在测试它的新功能。
在我的项目中,我使用Guava谓词和函数来使用Collections2.transform和Collections2.filter过滤和转换一些集合。
在这个迁移我需要改变例如番石榴代码java 8更改。所以,我做的改变是那种:
List naturals = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10,11,12,13);
Function duplicate = new Function(){
@Override
public Integer apply(Integer n)
{
return n * 2;
}
};
Collection result = Collections2.transform(naturals, duplicate);
至…
List result2 = naturals.stream()
.map(n -> n * 2)
.collect(Collectors.toList());
使用guava我是非常舒适的调试代码,因为我可以调试每个转换过程,但我的问题是如何调试例如.map(n – > n * 2)。
使用调试器我可以看到一些代码,如:
@Hidden
@DontInline
/** Interpretively invoke this form on the given arguments. */
Object interpretWithArguments(Object... argumentValues) throws Throwable {
if (TRACE_INTERPRETER)
return interpretWithArgumentsTracing(argumentValues);
checkInvocationCounter();
assert(arityCheck(argumentValues));
Object[] values = Arrays.copyOf(argumentValues, names.length);
for (int i = argumentValues.length; i < values.length; i++) {
values[i] = interpretName(names[i], values);
}
return (result < 0) ? null : values[result];
}
但是它不像Guava调试代码那么直接,实际上我找不到n * 2变换。
有没有办法看到这种转换或一种方法来调试这个代码?
编辑:我已经添加了来自不同的评论的答案和发布的答案
感谢Holger的意见回答了我的问题,使用lambda阻塞的方法允许我看到转换过程和调试在lambda体内发生了什么:
.map(
n -> {
Integer nr = n * 2;
return nr;
}
)
感谢Stuart Marks有方法引用的方法还允许我调试转换过程:
static int timesTwo(int n) {
Integer result = n * 2;
return result;
}
...
List result2 = naturals.stream()
.map(Java8Test::timesTwo)
.collect(Collectors.toList());
...
感谢Marlon Bernardes的回答我注意到我的Eclipse没有显示它应该和peek()的用法帮助显示结果。