Questions tagged [unmodifiable]

67 questions
50
votes
9 answers

Collections.unmodifiableList and defensive copy

If I write List a1 = Arrays.asList(1, 2, 3); List a2 = Collections.unmodifiableList(a1); a2 is read-only but if I write a1.set(0,10); then a2 is also modified. If in the API is said: Returns an unmodifiable view of the specified…
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
41
votes
5 answers

What does Collections.unmodifiableSet() do in Java?

I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but I don't understand why we can't just use the final modifier to accomplish this. In my understanding, final declares a constant: something that cannot be…
Roman
  • 124,451
  • 167
  • 349
  • 456
39
votes
3 answers

When is the unmodifiablemap (really) necessary?

I have a map of constants, like this: private static Map _typesMap = new HashMap() { { put ("string", 'S'); put ("normalizedString", 'N'); put ("token", 'T'); …
Paulo Guedes
  • 7,189
  • 5
  • 40
  • 60
24
votes
3 answers

Is there any performance risk to Collections.unmodifiableList?

I suggested returning Collections.unmodifiableList() instead of directly returning a member variable, and my colleague is concerned that there would be a performance hit. Of course the best answer is to measure it, and we may do that - but I'd like…
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
24
votes
7 answers

Java collections API: why are Unmodifiable[List|Set|Map] not publicly visible classes?

Collections.unmodifiableList(...) returns a new instance of a static inner class UnmodifiableList. Other unmodifiable collections classes are constructed same way. Were these classes public, one had two advantages: ability to indicate a more…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
19
votes
9 answers

Does the unmodifiable wrapper for java collections make them thread safe?

I need to make an ArrayList of ArrayLists thread safe. I also cannot have the client making changes to the collection. Will the unmodifiable wrapper make it thread safe or do I need two wrappers on the collection?
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
18
votes
3 answers

Returning an unmodifiable map

Using Collections.unmodifiableMap(...), I'm trying to return an unmodifiable view of a map. Let's say I have the following method, public final Map getMap(){ ... return Collections.unmodifiableMap(map); } Why is it legal elsewhere…
mre
  • 43,520
  • 33
  • 120
  • 170
18
votes
7 answers

Java unmodifiable array

final Integer[] arr={1,2,3}; arr[0]=3; System.out.println(Arrays.toString(arr)); I tried the above code to see whether a final array's variables can be reassigned[ans:it can be].I understand that by a final Integer[] array it means we cannot assign…
Emil
  • 13,577
  • 18
  • 69
  • 108
17
votes
7 answers

Unmodifiable List in java

I'm trying to set a List unmodifiable. In my code, I have a method which returns a list. This list shouldn't be modified, but I don't want to catch the exception returned by the unmodifiableList. private List listeReferenceSelectAll =…
user2346325
  • 201
  • 1
  • 2
  • 4
15
votes
6 answers

How to create a deep unmodifiable collection?

I often make a collection field unmodifiable before returning it from a getter method: private List _xs; .... List getXs(){ return Collections.unmodifiableList(_xs); } But I can't think of a convenient way of doing that if the X above is…
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
13
votes
1 answer

hashCode and equals for Collections.unmodifiableCollection()

The Collections class has a number of static helper methods to provide read-only views of various collection types, such as unmodifiableSet(), unmodifiableList(), etc. For these view objects, the hashCode() and equals() methods forward calls to the…
mergeconflict
  • 8,156
  • 34
  • 63
12
votes
2 answers

ConcurrentModificationException in unmodifiable collection

I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List filterCardsToDevice(Collection col) { final List
Pedro Estevão
  • 982
  • 1
  • 13
  • 41
10
votes
6 answers

How to return a thread safe/immutable Collection in Java?

In the project I am coding, I need to return a thread safe and immutable view from a function. However, I am unsure of this. Since synchronizedList and unmodifiableList just return views of a list, I don't know…
zw324
  • 26,764
  • 16
  • 85
  • 118
8
votes
3 answers

How to reverse an unmodifiable List in java

I am trying to reverse an unmodifiable list. However i have tried to achieve it but Is it possible to update or reverse and unmodifiable list? I know we can do it with Google Immutable List import java.util.*; public class ImmutableList { public…
vkrams
  • 7,267
  • 17
  • 79
  • 129
8
votes
1 answer

Unmodifiable lists in C#

In Java, one can use the Collections#unmodifiableList() method to create an unmodifiable list from an existing List object. Is there any counterpart in C# ? I'm new to the language and haven't been able to find anything like this in the MSDN docs.
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
1
2 3 4 5