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
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.