检查Java集合在运行时是否可修改

发布于 2025-02-09 15:35:42 字数 1393 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2025-02-16 15:35:42

,但我认为我们同意尝试捕获不是有效的控制流量工具。

这是有争议的。但不要辩论。

我缺少什么吗?

不。

[是否有]任何可以返回布尔值(是否可修改)或信号的接口进行修改 / unmodifiablity?< / p>

否。

没有标准的API方法或标记接口。 (我想您可以实施自己的...但是,在使用标准集合实施类时,您会遇到困难。)

您能做的最好的方法是对特定的实现类进行一些凌乱的测试,这些测试已知是可修改或非 -可修改。但是该方法也存在问题:

  • 您不能仅测试(私有)undifiableCollection等类。其他收集课程可能无法解码。

  • 即使在Java SE类库中,您还需要测试的类在不同的Java版本之间确实有所不同。 (请参阅 @Holger的评论!)

  • 您不能使用instanceof,因为有人可以将可修改类的类子类别用于无法解码...或反之亦然。

  • 可修改性可以取决于集合对象的运行时状态。例如,该对象可以使用add methods。

通常,最实际的解决方案是最实用的解决方案,无论您对使用例外情况如何正确的感觉如何。


如果当然...如果您对应用程序中使用的集合类具有全部控制,或者可能会遇到库...然后测试类 May 更可行。

But I think we agreed that try catch is not a valid control flow tool.

It is debatable. But lets not debate it.

Is there something I'm missing?

No.

[Is there] any method that returns a boolean (modifiable or not) or an interface that signals modifiablity / unmodifiablity?

No and No.

There is no standard API method or marker interface. (I guess you could implement your own ... but you would run into difficulties when using the standard collection implementation classes.)

The best you could do is to do some messy tests for specific implementation classes that are known to be modifiable or non-modifiable. But that approach has problems too:

  • You can't just test for the (private) UnmodifiableCollection etc classes. Other collection classes can be unmodifiable.

  • Even within the Java SE class library, the classes you would need to test for actually differs between different Java versions. (See @Holger's comment!)

  • You can't use instanceof because someone could subclass a modifiable class to be unmodifiable ... or vice versa.

  • Modifiability could depend on a collection object's runtime state. For example, the object could have a "frozen" flag that can be set after populating the collection using add methods.

In general, catching the exception is the most practical solution, irrespective of your feelings on how "proper" it is to use exceptions for this.


If course ... if you have total control over the collection classes that are used in your application, or may be encountered by your library ... then testing the classes may be more viable.

美羊羊 2025-02-16 15:35:42

尝试以下尝试:

if(yourcolletion.getClass().getSimpleName().equals("UnmodifiableCollection")) {
 //can not modified
} else {
//given list is modifiable
}

您还可以添加更多检查unodifiablelist等。

注意:我建议使用实例 而不是检查类名称但是您无法将undifiablelistundifiableCollection类导入代码

try below:

if(yourcolletion.getClass().getSimpleName().equals("UnmodifiableCollection")) {
 //can not modified
} else {
//given list is modifiable
}

you can also add for more check for UnmodifiableList etc.

Note: I would have recommended to use instanceof instead of checking for class name but you can not import the UnmodifiableList or UnmodifiableCollection classes into your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文