How to check type of implementation? How can you correct the last line so that it compiles and returns true?
List<String> list1 = new ArrayList<>(List.of("1", "2"));
List<String> list2 = List.of("1", "2").stream().toList();
list1 instanceof List<String>; // true
list2.getClass().getName(); // java.util.ImmutableCollections$ListN
list2 instanceof java.util.ImmutableCollections; // does not compile
How to check type of implementation? How can you correct the last line so that it compiles and returns true?
List<String> list1 = new ArrayList<>(List.of("1", "2"));
List<String> list2 = List.of("1", "2").stream().toList();
list1 instanceof List<String>; // true
list2.getClass().getName(); // java.util.ImmutableCollections$ListN
list2 instanceof java.util.ImmutableCollections; // does not compile
Share
Improve this question
edited Feb 17 at 10:51
Mark Rotteveel
109k227 gold badges156 silver badges220 bronze badges
asked Feb 17 at 8:59
MatthiasMatthias
2774 silver badges13 bronze badges
14
|
Show 9 more comments
1 Answer
Reset to default 12For a List to be immutable, it is necessary (although not sufficient) for it to have implemented all of the methods marked "optional" in the Javadoc to simply throw an UnsupportedOperationException
.
There is no safe way to check whether this is the case - the only mechanism would be to invoke all of the methods; but at least some of these would necessarily mutate the list if they don't throw the exception; plus, this would only tell you whether the list is unmodifiable, which isn't the same as immutable.
You could check for specific "known immutable" (or "known mutable") types using reflection (e.g. myList.getClass()....
); but you can't hope to cover all List implementations like that, since there are infinitely many possible implementations. In other words, you could reflectively detect some classes that you definitely know either to be mutable or immutable; but you still don't know in general.
You should in general assume that a List
argument to a method doesn't implement any of the optional methods, because there is no way of testing whether those methods are "really" implemented or will throw an NotImplementedException
.
If you actually need a mutable list, you should either:
- Copy the list parameter to a mutable structure (e.g. an Array list); or
- Change the type of the method parameter so that it will only accept mutable types.
ImmutableCollections
is a non-public Class in thejava.util
package, its javadoc states: "Container class for immutable collections. Not part of the public API. Mainly for namespace management and shared infrastructure." -ListN
is a class nested insideImmutableCollections
– user85421 Commented Feb 17 at 9:21java.util.ImmutableCollections
, or just "immutable" in general? – Sweeper Commented Feb 17 at 9:33