简体   繁体   English

java中不可修改列表的类型是什么

[英]What is the type of unmodifiable list in java

Unmodifiable list in java can be created as: java中的不可修改列表可以创建为:

List<String> unModifiableStringList = Collections.unmodifiableList(myActualModifiableList);

This is fine, but what is the actual runtime type of the list returned by the above function? 这很好,但是上面函数返回的列表的实际运行时类型是什么? How can we access that class? 我们怎样才能访问该课程? Is it even possible? 它甚至可能吗?

UPDATE: Actually I need to know at compile time that an unmodifiable list is being modified, Since I have to deal with a lot of lists, some of which are modifiable and others are not. 更新:实际上我需要在编译时知道正在修改一个不可修改的列表,因为我必须处理很多列表,其中一些是可修改的,而另一些则不是。 So it is very cumbersome to keep track? 跟踪是非常麻烦的吗?

Actually I need to know at compile time that an unmodifiable list is being modified. 实际上我需要在编译时知道正在修改一个不可修改的列表。

That is not possible. 这是不可能的。

Or at least, it is not possible without creating a completely different collections interface / class hierarchy. 或者至少,如果不创建完全不同的集合接口/类层次结构,则是不可能的。 And that's a bad idea because nothing designed to use regular collections would work with it. 这是一个坏主意,因为没有任何旨在使用常规集合的东西都可以使用它。

I suppose it would be possible to write a static code analyser that could detect this kind of thing ... in some cases ... but that's not strictly "compile time". 我想可以编写一个静态代码分析器来检测这种事情......在某些情况下......但这不是严格的“编译时间”。 Besides, I'm not aware of any existing static code analyser that does this "out of the box". 此外,我不知道任何现有的静态代码分析器可以“开箱即用”。


I wonder if there was a reason why they did it like this. 我想知道他们是否有这样做的理由。

Well none of the ways you might do this really work. 那么没有一个方法,你可以这样做真的管用。

Alternative #1: 备选方案#1:

 public interface UnmodifiableList<T> {
     public T get(int pos);
     ....
 }

 public interface List<T> extends UnmodifiableList<T> {
     public void add(T elem);
     ....
 }

While static typing can prevent us from using an unmodifiable list where a modifiable one is required, the reverse is not true. 虽然静态类型可以阻止我们使用不可修改的列表,其中需要可修改的列表,但反之则不然。 Every List is also an UnmodifiableList ... and that doesn't really make sense. 每个List也是一个UnmodifiableList ......这并没有多大意义。

Alternative 2: 备选方案2:

 public interface List <T> {
     public T get(int pos);
     public void add(T elem);
     ....
 }

 public interface UnmodifiableList<T> {
     // A marker interface
 }

Now static typing can prevent us from using an modifiable list where an umodifiable one is required, but not the reverse. 现在,静态类型可以阻止我们使用可修改的列表,其中需要一个可修改的列表,但不能反过来。 (That suits your requirement ...) Furthermore, a class that implements UnmodifiableList still inherits the add operation, and there's nothing to stop an application from trying to call it. (这符合您的要求...)此外,实现UnmodifiableList的类仍继承add操作,并且没有什么可以阻止应用程序尝试调用它。

In short, static type systems cannot adequately handle this kind of restriction. 简而言之,静态类型系统不能充分地处理这种限制。

Have you tried unModifiableStringList.getClass().getName() ? 你试过unModifiableStringList.getClass().getName()吗?

To me it gives 它给了我

java.util.Collections$UnmodifiableRandomAccessList

which, as seen from the source code, is a package-access static inner class of Collections . 从源代码中可以看出,它是一个包访问静态内部类的Collections

It is 它是

static class UnmodifiableList<E> extends UnmodifiableCollection<E>
                      implements List<E>

static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E>
                                      implements RandomAccess

static class UnmodifiableCollection<E> implements Collection<E>, Serializable

It is inner class of Collections , since Collections is non-instantiable and it is inner class with package visibility, you can not access the class, and it is the implementation hiding in OOP. 它是Collections内部类,因为Collections是不可实例化的,它是具有包可见性的内部类,你不能访问类,它是隐藏在OOP中的实现。

Debug shows that the runtime type is Collections.UnmodifiableRandomAccessList , so it's internal class. Debug显示运行时类型是Collections.UnmodifiableRandomAccessList ,因此它是内部类。 Code analysis also shows that it might be Collections.UnmodifiableList . 代码分析还显示它可能是Collections.UnmodifiableList

You shouldn't try to access this class, it's meant to be immutable. 你不应该尝试访问这个类,它应该是不可变的。 Try to use common interface instead, in this case = Collection . 尝试使用通用接口,在这种情况下= Collection

Your statement clearly says its still reference of List but it suppress modification of the list. 你的陈述清楚地说明了它仍然引用了List但它禁止修改列表。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#unmodifiableList(java.util.List ) http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#unmodifiableList(java.util.List

If you share unmodifiable list reference you make sure that nobody changes it and allow read only operations. 如果您共享不可修改的列表引用,请确保没有人更改它并允许只读操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM