unmodifiable

How inefficient is passing Collections.unmodifiable* an instance which is already wrapped with Collections.unmodifiable*?

萝らか妹 提交于 2019-12-04 03:42:12
问题 I have bits of piecework being done by different custom (source code unavailable) frameworks which hand back Map instances. Unfortunately, these frameworks are not consistent in their returning Map instances which have been wrapped with Collections.unmodifiableMap. To ensure a higher degree of immutability (for easier multi-threaded use) in my code, I have just uniformly called Collections.unmodifiableMap on anything returned by these frameworks. Map<String, Record> immutableMap = framework

Unmodifiable List in java

戏子无情 提交于 2019-12-01 15:16:41
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<T> listeReferenceSelectAll = null; List<T> oListeRet = new ArrayList<T>(); oListeRet = listeReferenceSelectAll; return new ArrayList<T>(oListeRet); It is an existing code and I have to transform it to return an unmodifiable list, but if an "add" method has been called, no exception has to be caught. First I have create a class which implements List to override "add" method to log the

ConcurrentModificationException in unmodifiable collection [duplicate]

↘锁芯ラ 提交于 2019-12-01 03:31:02
This question already has an answer here: Why is a ConcurrentModificationException thrown and how to debug it 6 answers I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List<MyClass> filterCardsToDevice(Collection<MyClass> col) { final List<MyClass> newList = new ArrayList<MyClass>(); for (MyClass myObj : col) { long id = myObj.getId(); if (id < 0 || id > 0xFFFFFFFFl) { // just a log here } else { newList.add(myObj); } } return newList; } private final Map<Long, MyClass> map = new

Returning an unmodifiable map

空扰寡人 提交于 2019-12-01 02:25:52
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<Foo, Bar> getMap(){ ... return Collections.unmodifiableMap(map); } Why is it legal elsewhere to do the following, Map<Foo, Bar> map = getMap(); map.put(...); This doesn't throw an UnsupportedOperationException like I thought it would. Can someone please explain this, or suggest how I can successfully return a truly unmodifiable map? Are you sure you're not masking your exceptions somehow? This works absolutely fine, in that it throws

Returning an unmodifiable map

这一生的挚爱 提交于 2019-11-30 22:02:39
问题 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<Foo, Bar> getMap(){ ... return Collections.unmodifiableMap(map); } Why is it legal elsewhere to do the following, Map<Foo, Bar> map = getMap(); map.put(...); This doesn't throw an UnsupportedOperationException like I thought it would. Can someone please explain this, or suggest how I can successfully return a truly unmodifiable map? 回答1: Are you

Collections.unmodifiableList and defensive copy

帅比萌擦擦* 提交于 2019-11-28 17:26:13
If I write List<Integer> a1 = Arrays.asList(1, 2, 3); List<Integer> 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 collection. This method allows modules to provide users with "read-only" access to internal collections. then, why if I modify the original collection also the target-copied collection is modified? Maybe did I misunderstand the meaning and if so what's the way to write a defensive copy of that collection? Joachim Sauer Yes, you understood it correctly

Why is my `unmodifiableList` modifiable? [duplicate]

≡放荡痞女 提交于 2019-11-28 13:37:30
This question already has an answer here: Why can we change the unmodifiable list if we have the original one? 7 answers I want a List whose elements cannot be removed nor added. I thought I'd found the answer with Collections.unmodifiableList in Java 8. I pass my original list and get back a supposedly unmodifiable list. Yet when I delete an element from the original list, my unmodifiable list is modified. What is going on? See this demo code. My unmodifiable list shrinks from 3 elements 2 when deleting from original. String dog = "dog"; String cat = "cat"; String bird = "bird"; List< String

Java unmodifiable array

给你一囗甜甜゛ 提交于 2019-11-28 11:00:19
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 another instance of Integer[] apart from the one we have assigned initially.I would like to know if whether it is possible to make the array variables also unmodifiable. Andre Holzner This isn't possible as far as I know. There is however a method Collections.unmodifiableList(..) which creates an unmodifiable view of e.g. a List<Integer> . If you

Collections.unmodifiableList and defensive copy

自作多情 提交于 2019-11-27 10:27:58
问题 If I write List<Integer> a1 = Arrays.asList(1, 2, 3); List<Integer> 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 collection. This method allows modules to provide users with "read-only" access to internal collections. then, why if I modify the original collection also the target-copied collection is modified? Maybe did I misunderstand the meaning and if so what

Why is my `unmodifiableList` modifiable? [duplicate]

夙愿已清 提交于 2019-11-27 07:48:01
问题 This question already has answers here : Why can we change the unmodifiable list if we have the original one? (7 answers) Closed 4 years ago . I want a List whose elements cannot be removed nor added. I thought I'd found the answer with Collections.unmodifiableList in Java 8. I pass my original list and get back a supposedly unmodifiable list. Yet when I delete an element from the original list, my unmodifiable list is modified. What is going on? See this demo code. My unmodifiable list