java java中不可修改列表的类型是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10480173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the type of unmodifiable list in java
提问by Stephen C
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?
更新:实际上我需要在编译时知道一个不可修改的列表正在被修改,因为我必须处理很多列表,其中一些是可修改的,而另一些则不是。所以跟踪起来很麻烦?
回答by Stephen C
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 somecases ... 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 mightdo 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.
简而言之,静态类型系统无法充分处理这种限制。
回答by Péter T?r?k
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
.
回答by Pau Kiat Wee
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 notaccess the class, and it is the implementation hiding in OOP.
它是 的内部类Collections
,因为它Collections
是不可实例化的,并且是具有包可见性的内部类,您无法访问该类,并且它是隐藏在 OOP 中的实现。
回答by Greg Kopff
请参阅:http: //grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collections.java#Collections.unmodifiableList%28java.util.List%29
It's an inner class in Collections
:
这是一个内部类Collections
:
static class UnmodifiableList<E> extends UnmodifiableCollection<E>
implements List<E>
回答by ?ukaszBachman
Debug shows that the runtime type is Collections.UnmodifiableRandomAccessList
, so it's internal class. Code analysis also shows that it might be Collections.UnmodifiableList
.
Debug 显示运行时类型是Collections.UnmodifiableRandomAccessList
,所以它是内部类。代码分析也表明它可能是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
。
回答by neo
Your statement clearly says its still reference of List but it suppress modification of the list.
您的声明清楚地表明它仍然引用 List,但它禁止修改列表。
If you share unmodifiable list reference you make sure that nobody changes it and allow read only operations.
如果您共享不可修改的列表引用,请确保没有人更改它并允许只读操作。