I've discovered a problem compiling a class that happens only in maven, not inside Eclipse.
The code is the following:
@Test
public void compliationFailOnMaven() {
Optional<List<String>> list = getDummyList();
List<Integer> hascodes = list.orElse(Collections.EMPTY_LIST).stream().map(value -> value.hashCode()).collect(toList());
assertThat(hascodes).isNotNull();
}
private Optional<List<String>> getDummyList() {
return Optional.ofNullable(new ArrayList<String>(0));
}
If you insert this code into a maven project and try to execute it using mvn clean test
it fails due a compilation problem:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /java-tests/general-test/src/test/java/com/java8/stream/StreamTest.java:[113,125] incompatible types: java.lang.Object cannot be converted to java.util.List<java.lang.Integer>
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.844 s
[INFO] Finished at: 2015-11-19T18:07:36+01:00
[INFO] Final Memory: 26M/277M
[INFO] ------------------------------------------------------------------------
However, if you import this project into eclipse, it can compile and execute it without any problem.
The problem is the use of generics. If I replace this line:
List<Integer> hascodes = list.orElse(Collections.EMPTY_LIST).stream().map(value -> value.hashCode()).collect(toList());
with this one:
List<Integer> hascodes = list.orElse(new ArrayList<String>(0)).stream().map(value -> value.hashCode()).collect(toList());
Everything works in both environments: eclipse and maven.
Does anybody know why it's happening this?
Why are they producing different results?
I only have one JVM installed, so both are using the same Java version