最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Check if List is immutable - Stack Overflow

programmeradmin3浏览0评论

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
  • 3 Can you elaborate a bit why you need to check/verify that / what you are using this for? Do you meed to check actual immutability or just unmodifiability? – dan1st Commented Feb 17 at 9:07
  • 1 ImmutableCollections is a non-public Class in the java.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 inside ImmutableCollections – user85421 Commented Feb 17 at 9:21
  • 5 This sounds like a XY Problem. Do you specifically want to check if it's one of the classes in java.util.ImmutableCollections, or just "immutable" in general? – Sweeper Commented Feb 17 at 9:33
  • 1 there is no need to know it, just because the lists are arguments of a function?! – user85421 Commented Feb 17 at 9:38
  • 6 If your function requires mutable lists, treat them as mutable and don't pass in an immutable list. If your function doesn't require mutable lists, you probably shouldn't be mutating them even if you happen to receive mutable lists. – khelwood Commented Feb 17 at 10:27
 |  Show 9 more comments

1 Answer 1

Reset to default 12

For 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.
发布评论

评论列表(0)

  1. 暂无评论