java lambda map用法_java – 如何调试stream() map(…)与lambda表达式?

在我们的项目中,我们正在迁移到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()的用法帮助显示结果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值