What is the type of non-modifiable list in java

Unmodifiable list in java can be created as:

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

This is normal, but what is the actual type of runtime of the list returned by the specified function? How can we access this class? Is it possible?

UPDATE: Actually, I need to know at compile time that the changed list changes, since I have to deal with a large number of lists, some of which are modifiable, and others not. So what is very difficult to track?

+5
source share
6 answers

Actually, I need to know at compile time that the modified list is changing.

It's impossible.

, , /. , , , .

, , ... ... " ". , , " ".


, , .

, .

№1:

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

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

, , . UnmodifiableList... .

2:

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

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

, , . ( ...) , , UnmodifiableList, - add, , .

, .

+8

unModifiableStringList.getClass().getName()?

java.util.Collections$UnmodifiableRandomAccessList

, , Collections.

+3

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

Collections, Collections , , , .

+3
+1

, Collections.UnmodifiableRandomAccessList, . , Collections.UnmodifiableList.

, . , = Collection.

0

List, .

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

If you share a link to an unmodifiable list, you will make sure that no one changes it and allows read-only operations.

0
source

All Articles