I am running a small code to test the reduce()
functionality of Java8 lambda.
I have a list of objects of type Person
having fields name
and age
. I am trying to find the sum of ages of all persons using reduce functionality of Java8. The code is given below:
public class Person {
String name;
Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class TestReduce {
public static void main(String[] args){
Person p1 = new Person("P1", 10);
Person p2 = new Person("P2", 20);
Person p3 = new Person("P3", 30);
List<Person> personList = new ArrayList<Person>();
personList.add(p1);
personList.add(p2);
personList.add(p3);
int sumAge = personList.stream()
.reduce(0, (result, person) -> result += person.age, Integer::sum);
}
}
When I run the above code, I am getting the following error although my IDE does not show any compilation error.
Using javac 1.8.0_25 to compile java sources
java: An exception has occurred in the compiler (1.8.0_25). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java: java.lang.NullPointerException
java: at com.sun.tools.javac.code.Types.isConvertible(Types.java:290)
java: at com.sun.tools.javac.comp.Check.assertConvertible(Check.java:922)
java: at com.sun.tools.javac.comp.Check.checkMethod(Check.java:876)
java: at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3838)
java: at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3615)
java: at com.sun.tools.javac.comp.Attr.checkMethodIdInternal(Attr.java:3522)
java: at com.sun.tools.javac.comp.Attr.checkMethodId(Attr.java:3501)
java: at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3488)
java: at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:3370)
java: at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1897)
java: at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
java: at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1843)
java: at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
java: at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:649)
java: at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1093)
java: at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
java: at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
java: at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
java: at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
java: at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
java: at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1035)
java: at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
java: at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4342)
java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4252)
java: at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4181)
java: at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4156)
java: at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
java: at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
java: at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
java: at com.sun.tools.javac.main.Main.compile(Main.java:523)
java: at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
java: at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
java: at org.jetbrains.jps.javac.JavacMain.compile(JavacMain.java:165)
java: at org.jetbrains.jps.incremental.java.JavaBuilder.compileJava(JavaBuilder.java:407)
java: at org.jetbrains.jps.incremental.java.JavaBuilder.compile(JavaBuilder.java:304)
java: at org.jetbrains.jps.incremental.java.JavaBuilder.doBuild(JavaBuilder.java:210)
java: at org.jetbrains.jps.incremental.java.JavaBuilder.build(JavaBuilder.java:182)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1106)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:814)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:862)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:777)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:600)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:352)
java: at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:184)
java: at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:129)
java: at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:224)
java: at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:113)
java: at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:157)
java: at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
java: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
java: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java: at java.lang.Thread.run(Thread.java:745)
java: Errors occurred while compiling module 'Lambda'
Compilation completed with 1 error and 0 warnings in 2 sec
1 error
0 warnings
Error:java: Compilation failed: internal java compiler error
This is known bug in javac compiler. Either update the compiler to version 1.8.0_40 (at least) or specify the lambda argument types explicitly ((Integer result, Person person) -> ...
).
Although Tagir's answer more directly answers your question (about the compiler error), it's worth pointing out that there's a better way to code your solution that is not only far cleaner and simpler, but would not encounter that error:
int sumAge = personList.stream().mapToInt(Person::age).sum();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With