What Is the Alternative of List. Of() in Java If I'm Using Java 8 Using Sts
I'm Trying to Create One Spring Boot Application Through Sts4 but While I'm Using List. Of() It Never Give Any Suggestion Like This Due to Which Getting...
I'm trying to create one Spring Boot Application through STS4 but while I'm using List.of() it never give any suggestion like this due to which getting {beans.factory.UnsatisfiedDependencyException} kind of exception
1 Answer
Just bear in mind the following difference:
List.ofwas introduced with Java 9, and it returns an unmodifiable List. The list returned by the method cannot be resized nor modified, which means, you cannot add, remove or replace any element either on the list or its iterator.Arrays.asList: This returns instead a not resizable List, which means that you can replace an element of the list but cannot remove or add any element.
With Java 8, the best option to get the same thing as List.of is to use the static method Collections.unmodifiableList. You can use it just to wrap your list, so every time you try to do any operation on the list or on its iterator (i.e. set, add, remove) an UnsupportedOperationException is thrown. Here is an example related to a List of String.
List<String> newUnmodList = Collections.unmodifiableList(oldList);