1

I am using java 11, using sts IDE, I compile and run the springboot application fine from the IDE, but when I compile it from command line using mvn

 mvn clean verify

I got this error

cannot find symbol [ERROR]   symbol:   method toList()

the code snippet is

......
......
return addressRepository.getAddressesBySystemUserId(systemUserId).stream().map(e -> {
            AddressDto dto = null;
            dto = AddressMapper.mapAddressToAddressDto(e);
            return dto;
        }).toList();

......

snippet of the pom file

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
</properties>
Ahmed Hasan
  • 31
  • 2
  • 8
  • 1
    use `.collect(Collectors.toList())` instead – Omkar76 Feb 22 '22 at 10:52
  • 2
    `Stream.toList()` is introduced in Java 16. So you don't have it in Java 11 – ernest_k Feb 22 '22 at 10:52
  • But why the above code is working well while compiling/running the app using STS IDE, i already mentioned in the pom file file to use jdk 11, it seems that the STS IDE uses different version (not jdk 11 which i mentioned in the pom) – Ahmed Hasan Feb 22 '22 at 11:39

1 Answers1

5

You need to use .collect() in order to collect list from stream in Java 11. In your case :

return addressRepository.getAddressesBySystemUserId(systemUserId).stream().map(e -> {
            AddressDto dto = null;
            dto = AddressMapper.mapAddressToAddressDto(e);
            return dto;
        }).collect(Collectors.toList());

fukit0
  • 924
  • 1
  • 11
  • 35
  • Yes, I wanted to point out that you must use `.collect()` instead of `.toList()` – fukit0 Feb 22 '22 at 11:21
  • But why the above code is working well while compiling/running the app using STS IDE, i already mentioned in the pom file file to use jdk 11, it seems that the STS IDE uses different version (not jdk 11 which i mentioned in the pom) – Ahmed Hasan Feb 22 '22 at 11:40
  • What is the version at Java Build Path? – fukit0 Feb 22 '22 at 12:59
  • I have edited the post by adding the snippet of the pom file where i have mentioned that the compiler source and target are 11. – Ahmed Hasan Feb 22 '22 at 14:39
  • IDE's jdk version and your build tool's java version are different things. I presume your IDE's selected SDK is not 11. Please check it out. – fukit0 Feb 23 '22 at 06:42