0

I want to create an unmodifiable list but I get the following error. Current JDK that I'm using is 11.

package com.example.demo;


import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        List<Integer> result = Stream.of(1, 2, 3, 4)
                .collect(Collectors.toUnmodifiableList());
    }

}

Output:

java: cannot find symbol
  symbol:   method toUnmodifiableList()
  location: class java.util.stream.Collectors

enter image description here

Update:

I get this error just for toUnmodifiableList() when I run Main.java class inside my SpringBoot project as a separate java file, but changing toUnmodifiableList() to toList() method works fine.

List<Integer> result = Stream.of(1, 2, 3, 4)
                .collect(Collectors.toList()); // this works

Then, I created a simple Java project and there both methods work fine.

enter image description here

Davoud
  • 2,576
  • 1
  • 32
  • 53
  • Does [this thread](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) help you? – crissal Feb 01 '21 at 15:22
  • How are you running an compiling the program? – matt Feb 01 '21 at 15:23
  • I can't reproduce the error. – Unmitigated Feb 01 '21 at 15:32
  • 1
    This is a build issue. Have you set Java version in your build file? – Abhijit Sarkar Feb 01 '21 at 15:55
  • 1
    This suggests you're building with either Java 8, or with Java 10 or higher with `-release 8` or `-release 9`. The warning in IntelliJ is because you have the language level of your project set to 9 or lower. Given you seem to be using Maven (given the `target` directory in one of your screenshots), you likely haven't explicitly configured the source/target Java version, or it is explicitly configured on a lower Java version. It would be helpful if you provide a [mre] including a pom.xml. – Mark Rotteveel Feb 01 '21 at 16:18

2 Answers2

1

Check those 2 places on Intelij and make sure of proper java version

enter image description here

enter image description here

Using JDK 15 I can't reproduce the error. So it must have to do with your java configurations

enter image description here

Check on the second image there where it says Project language level (It is next to Project SDK level). That was what Intelij configured. Try to modify it again and see the error.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
0

The command mvn clean install caused Intellij to show me the problem.

enter image description here

The problem was the Language level in Project Structures -> Modules which I had to set to 10 or above.

enter image description here

Davoud
  • 2,576
  • 1
  • 32
  • 53