In our project, we are switching to java 8, and we are testing its new features.
In my project, I use Guava predicates and functions to filter and transform some collections using Collections2.transform and Collections2.filter .
In this migration, I need to change, for example, the guava code to java 8 changes. So the changes I make are as follows:
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);
For...
List<Integer> result2 = naturals.stream() .map(n -> n * 2) .collect(Collectors.toList());
Using guava I was very convenient for debugging the code, since I could debug every conversion process, but my problem is how to debug, for example .map(n -> n*2) .
Using the debugger, I see code like:
@Hidden @DontInline 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]; }
But it is not as scary as Guava for debugging code, in fact I could not find the n * 2 transform.
Is there a way to see this conversion or a way to easily debug this code?
EDIT: I added an answer from various comments and posted the answers
Thanks to a comment by Holger that answered my question, the approach with the lambda block allowed me to see the transformation process and debug what happened inside the lambda body:
.map( n -> { Integer nr = n * 2; return nr; } )
Thanks to Stuart Marks method-referenced approach also allowed me to debug the conversion process:
static int timesTwo(int n) { Integer result = n * 2; return result; } ... List<Integer> result2 = naturals.stream() .map(Java8Test::timesTwo) .collect(Collectors.toList()); ...
Thanks to Marlon Bernardes answer, I noticed that my Eclipse does not show what it needs, and using peek () helped display the results.