unmodifiable

Can the list returned by a Java streams collector be made unmodifiable? [duplicate]

时光怂恿深爱的人放手 提交于 2020-06-17 03:47:07
问题 This question already has answers here : Creating an immutable list from an existing list using streams (2 answers) Closed 2 months ago . When working with Java streams, we can use a collector to produce a collection such as a stream. For example, here we make a stream of the Month enum objects, and for each one generate a String holding the localized name of the month. We collect the results into a List of type String by calling Collectors.toList(). List < String > monthNames = Arrays

ConcurrentModificationException in unmodifiable collection [duplicate]

穿精又带淫゛_ 提交于 2019-12-30 08:25:12
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . 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 >

ConcurrentModificationException in unmodifiable collection [duplicate]

一曲冷凌霜 提交于 2019-12-30 08:24:09
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . 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 >

Defects of Immutable collections of Guava?

这一生的挚爱 提交于 2019-12-23 05:26:35
问题 I am not sure the defects of Immutable collections I understand is correct, so I list them in this answer. Hope someone corrects me here. a): Comparing to Collections.unmodifiableXXX(), ImmutableXXX.copyOf() loses the source collection feature . For example, when a linkedList is put into ImmutableList.copyOf(), the ImmutableList is not linked anymore. Same as Tree based collection. b): People think Collections.unmodifiableXXX just uses same reference of source collection, so once the source

hashCode and equals for Collections.unmodifiableCollection()

为君一笑 提交于 2019-12-21 07:16:35
问题 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 underlying collection... With one odd exception: unmodifiableCollection() . The JavaDoc explicitly states: The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object 's equals

How to create a deep unmodifiable collection?

别说谁变了你拦得住时间么 提交于 2019-12-21 03:42:48
问题 I often make a collection field unmodifiable before returning it from a getter method: private List<X> _xs; .... List<X> getXs(){ return Collections.unmodifiableList(_xs); } But I can't think of a convenient way of doing that if the X above is itself a List: private List<List<Y>> _yLists; ..... List<List<Y>> getYLists() { return Collections.unmodifiableList(_yLists); } The problem in the above is of course that though the client cannot modify the List of lists, it can add/delete Y objects

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

人走茶凉 提交于 2019-12-18 12:14:15
问题 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? 回答1: It depends. The wrapper will only prevent changes to the collection it wraps, not to the objects in the collection. If you have an ArrayList of ArrayLists, the global List as well as each of its element Lists need to be wrapped separately, and you may also have to do something

Unmodifiable List in java

情到浓时终转凉″ 提交于 2019-12-18 07:36:53
问题 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

How to create a Java Map<String,String> with unmodifiable keys?

吃可爱长大的小学妹 提交于 2019-12-11 01:59:59
问题 In java, how should I create a Map<String,String> that has unmodifiable keys, while keeping the values modifiable. I'd like to hand this Map<String,String> through an interface for someone else to add/change the Map values, but not be able to change the Map keys. The background on higher level problem is that I have list/set of variable names (with tree like structure) (represented as java String) that I'd like code on the other side of the java interface to be able to populate aliases (also

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

倖福魔咒の 提交于 2019-12-07 01:45:32
问题 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 if Collections.synchronizedList(Collections.unmodifiableList(this.data)); would do the trick. Could anyone tell me if this is correct, and in case it is not, are there any situations that this would likely to fail? Thanks for any inputs! 回答1: I find this to be a real gap in the JDK.