在我们的项目中,我们正在迁移到Java 8,我们正在测试它的新功能.
在我的项目中,我使用Guava谓词和函数来使用Collections2.transform和过滤和转换一些集合Collections2.filter.
在这次迁移中,我需要将例如guava代码更改为java 8更改.所以,我正在做的改变是这样的:
List<Integer> naturals = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10,11,12,13);
Function <Integer, Integer> duplicate = new Function<Integer, Integer>(){
@Override
public Integer apply(Integer n)
{
return n * 2;
}
};
Collection result = Collections2.transform(naturals, duplicate);
Run Code Online (Sandbox Code Playgroud)
至...
List<Integer> result2 = naturals.stream()
.map(n -> n * 2)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
使用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[] …Run Code Online (Sandbox Code Playgroud)